Reputation: 12988
I know that we have a lot of topics about xml but i cant get this to work. I have this xml:
<OrderRoot>
<Order>
<Client>Company Company</Client>
<Cod>0000</Cod>
</Order>
<Itens>
<Item Sequence="01">
<Product>54321</Product>
<Description>xxxxxxx</Description>
</Item>
<Item Sequence="02">
<Product>12345</Product>
<Description>xxxxxxx</Description>
</Item>
<Item Sequence="03">
<Product>123456</Product>
<Description>xxxxxxx</Description>
</Item>
</Itens>
</OrderRoot>
My Code:
order.Client = xmlDocument.Descendants("Client").First().Value;
order.A1_Codigo = xmlDocument.Descendants("Cod").First().Value;
foreach (XElement item in xmlDocument.Descendants("Itens"))
{
//EDITed
var aux = item.Element("Product").Value; //Get the null reference exception here.
}
But i get always system null reference in the foreach loop. The order before works fine.
What im doing wrong here?
Upvotes: 0
Views: 421
Reputation: 125660
Product is not a direct child of Itens, and that's why you're getting an exception. Try changing you loop source:
foreach (XElement item in xmlDocument.Descendants("Itens").Elements("Item"))
Upvotes: 1