我正在使用以下查询将 XML 转换为结构。如果一个元素不存在,我将如何在以下查询中适应它(ClntCde 将始终存在,但标题可能不存在)
ClientTbl ClientData = clientNode.Select(x => new ClientTbl
{
ClientCode = x.Elements().Where(e => e.Name.LocalName == "ClntCde").FirstOrDefault().Value,
//Title = x.Elements().Where(e => e.Name.LocalName == "Title").ToString(),
//Title = x.Elements().Where(e => e.Name.LocalName == "Title").FirstOrDefault().Value,
//Title = x.Elements("Title").FirstOrDefault().Value,
}
).First();
XML:
<Client xmlns="http://www.website.co.za/namespace">
<ClntCde>0000013</ClntCde>
<Surname>Account Holder Surname</Surname>
<Address>
<Addr1>.</Addr1>
<Addr2>.</Addr2>
<Addr3>.</Addr3>
<Addr4>.</Addr4>
<PostCde>2000</PostCde>
</Address>
<ClntAccTyp>A</ClntAccTyp>
<ElecInd>NO</ElecInd>
<Lang>ENG</Lang>
</Client>