0
如何獲得第一個狀態<StatusType>
元素<Code>
是D.此LINQ工作,但重新說明x.Element("StatusType")
可能的nullreferenceexpection。嵌套的XML元素拋出異常
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>