【发布时间】:2015-06-30 03:20:31
【问题描述】:
如何通过其<StatusType> 元素<Code> 获得第一个状态是 D。此 LINQ 有效,但重新明确指出 x.Element("StatusType") 可能为 nullreferenceexpection。
var aaa = track.Descendants("Status")
.SingleOrDefault(x => x.Element("StatusType") != null &&
x.Element("StatusType").Element("Code").Value == "D");
XML:-
<Activity>
<ActivityLocation>
<Address>
<City>WILSLLE</City>
<StateProvinceCode>oR</StateProvinceCode>
<PostalCode>978880</PostalCode>
<CountryCode>US</CountryCode>
</Address>
<Code>M7</Code>
<Description>RECEIVER</Description>
<SignedForByName>abc</SignedForByName>
</ActivityLocation>
<Status>
<StatusType>
<Code>D</Code>
<Description>DELIVERED</Description>
</StatusType>
<StatusCode>
<Code>KB</Code>
</StatusCode>
</Status>
<Date>20150504</Date>
<Time>085100</Time>
</Activity>
<Activity>
<ActivityLocation>
<Address>
<City>TUALATIN</City>
<StateProvinceCode>OR</StateProvinceCode>
<CountryCode>US</CountryCode>
</Address>
</ActivityLocation>
<Status>
<StatusType>
<Description>OUT FOR DELIVERY</Description>
</StatusType>
<StatusCode>
<Code>DS</Code>
</StatusCode>
</Status>
<Date>20150504</Date>
<Time>045600</Time>
</Activity>
【问题讨论】:
标签: c# linq linq-to-xml