Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获取XDocument的值_C#_.net_Xml_Linq To Xml - Fatal编程技术网

C# 获取XDocument的值

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

我知道我们有很多关于xml的话题,但我无法让它发挥作用。 我有以下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>
但我总是在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"))