C# 获取XDocument的值
我知道我们有很多关于xml的话题,但我无法让它发挥作用。 我有以下xml:C# 获取XDocument的值,c#,.net,xml,linq-to-xml,C#,.net,Xml,Linq To Xml,我知道我们有很多关于xml的话题,但我无法让它发挥作用。 我有以下xml: <OrderRoot> <Order> <Client>Company Company</Client> <Cod>0000</Cod> </Order> <Itens> <Item Sequence="01"> <Product>54321
<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>
但我总是在foreach循环中得到系统空引用。之前的订单很好用
我做错了什么?产品不是Itens的直接产物,这就是为什么您会遇到例外。尝试更改循环源:
foreach (XElement item in xmlDocument.Descendants("Itens").Elements("Item"))
pedido
的确切定义是什么?因为现在,这是你的空引用。对不起,我编辑了。Order是一个对象,里面有一个iten列表和其他属性。好的,调试你的代码,找出什么是null以及为什么。有这么一个小窗口,我们很难告诉你出了什么问题。您甚至还没有告诉我们哪一行和哪一个对象是空的。您是否按顺序实例化了Itens
属性?是的,我在构造函数中有它。
foreach (XElement item in xmlDocument.Descendants("Itens").Elements("Item"))