Getting xml parts uing Linq to XML
This XML snippet below is part of a much larger XML. I've tried over and over to get the address bar 1, 2,3, city and state, zip code and country. I want to function so I can pick these addresses based on InvoiceHeader id = "XXXX" however I keep bumping into walls. I have tried the following query below or something nearby, but I keep getting an error object reference not set to an object instance.
here is my request 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>
<Supplier id="3825405">
<ContactInfo type="main">
<Address>
<AddressLine lineNumber="1">Post </AddressLine>
<AddressLine lineNumber="2">30 Street</AddressLine>
<AddressLine lineNumber="3">30 Street</AddressLine>
<City>Saint Louis</City>
<State>MO</State>
<PostalCode>63103-2530</PostalCode>
<Country isoCountryCode="US">United States</Country>
</Address>
</ContactInfo>
</Supplier>
<BillTo>
<Address>
<AddressLine lineNumber="1">vvvv</AddressLine>
<AddressLine lineNumber="2">vvvv</AddressLine>
<City>Philadelphia</City>
<State>PA</State>
<PostalCode>19222</PostalCode>
<Country isoCountryCode="US">United States</Country>
</Address>
</BillTo>
<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>
source to share
Your element range variable corresponds to an Address element that has no id attribute. Instead, you need a query that first finds (or filters) the matching InvoiceHeader and then looks for the handles to the Address element of the matching InvoiceHeader.
Here's an example of searching for InvoiceHeader:
var Header = PurchaseOrderXml.Descendants("InvoiceHeader")
.FirstOrDefault(header => (string)header.Attribute("id").Value == headerId);
You can check if the header is found ( Header != null
). Once you have a title, do whatever you need to do within that element. Example:
var RemitToAddress = Header.Descendants("RemitTo").Descendants("Address").FirstOrDefault();
You might want to check for other elements in the header, and so breaking down requests like this makes your intent clear.
Also note that I used Descendants
, but you can also use Elements
if it matches your schema better.
Another example: to get the elements AddressLine
and combine them, you can try something line by line:
IEnumerable<string> AddressLines = RemitToAddress.Elements("AddressLine")
.OrderBy(line => (int)line.Attribute("lineNumber"))
.Select(line => line.Value);
var AddressText = string.Join("\n", AddressLines);
source to share
Try this: -
var result = xdoc.Root.Descendants("InvoiceHeader")
.Where(x => x.Attribute("id").Value == "23951768")
.SelectMany(x => x.Descendants("Address"))
.Select(x =>
{
{
var addressLine1 = x.Elements("AddressLine")
.FirstOrDefault(z => z.Attribute("lineNumber").Value == "1");
var addressLine2 = x.Elements("AddressLine")
.FirstOrDefault(z => z.Attribute("lineNumber").Value == "2");
var addressLine3 = x.Elements("AddressLine")
.FirstOrDefault(z => z.Attribute("lineNumber").Value == "3");
return new
{
AddressLine1 = addressLine1 != null ? addressLine1.Value : String.Empty,
AddressLine2 = addressLine2 != null ? addressLine1.Value : String.Empty,
AddressLine3 = addressLine3 != null ? addressLine1.Value : String.Empty,
City = x.Element("City").Value,
State = x.Element("State").Value
PostalCode = x.Element("PostalCode").Value,
Country= x.Element("Country").Value,
};
}
});
source to share