Retrieving xml parts uing Linq to XML



This snippet of XML below is part of a much larger XML. I have tried over and over to get address line 1, 2,3 and City and State, postal code and country. I want to functionalize this so I can pick out those addresses based on InvoiceHeader id="XXXX" however I keep running into a wall. I have tried the following query below or something along the lines but I keep getting an error object reference not set to an instance of an object.


here is my query can someone please point out my obvious mistake.



IEnumerable<string> partNos =
from item in PurchaseOrderXml.Descendants("RemitTo").Descendants("Address")
where (string)item.Attribute("id").Value == "23951768"
select (string)item;



<Invoice>
<InvoiceHeader id="23951768" status="InProcess">
<InvoiceName />
<InvoiceNumber>23951768</InvoiceNumber>
<InvoiceDate>2014-09-26 00:00:00.0</InvoiceDate>
<DueDate>2014-10-26 00:00:00.0</DueDate>
<SupplierInvoiceNo>534254504</SupplierInvoiceNo>
<InvoiceType>Invoice</InvoiceType>
<RemitTo>
<Address>
<AddressLine lineNumber="1">P O BOX 535182</AddressLine>
<AddressLine lineNumber="2" />
<AddressLine lineNumber="3" />
<City>ATLANTA</City>
<State>GA</State>
<PostalCode>303535182</PostalCode>
<Country isoCountryCode="US">United States</Country>
</Address>
</RemitTo>
</InvoiceHeader>
</Invoice>

No comments:

Post a Comment