【问题标题】:LINQ to XML - Reading XML DocumentLINQ to XML - 读取 XML 文档
【发布时间】:2011-05-29 11:37:41
【问题描述】:

有人可以帮我如何使用 Linq to XML 阅读下面的 XML 文档吗?

<?xml version='1.0' encoding='UTF-8' ?>
<cXML>
<Request>
<OrderRequest>
  <OrderRequestHeader orderID="xy1234" orderDate="2007-01-1T15:5400+10:00" type="new" orderVersion="001">
    <Total>
      <Money currency="NZ">34.06</Money>
    </Total>
    <ShipTo>
      <Address>
        <Name xml:lang="en">xyz</Name>
        <PostalAddress name="xyz">
          <Street>xyz street</Street>
          <City>xyz</City>              
        </PostalAddress>
      </Address>
    </ShipTo>
    <BillTo>
      <Address>
        <Name xml:lang="en">XYZ</Name>
        <PostalAddress name="XYZ">
          <Street>PO BOX 1234</Street>
          <City>xyz</City>
          <State>xyz state</State>              
        </PostalAddress>
      </Address>
    </BillTo>
    <Contact role="xxx" addressID="123456789">
      <Name xml:lang="en">XYZ</Name>
      <Email name="business">XYZ@ms.com.nz</Email>
      <Phone>
        <TelephoneNumber>
          <Number>1234-1234</Number>
        </TelephoneNumber>
      </Phone>
    </Contact>
    <Contact role="ShipTo">
      <Name xml:lang="en">XYZ</Name>
    </Contact>
    <Contact role="Supplier">
      <Name xml:lang="en">XYZ pty ltd</Name>
    </Contact>
  </OrderRequestHeader>
  <ItemOut quantity="20" lineNumber="1" requestedDeliveryDate="2007-01-01T00:0000+10:00">
    <ItemID>
      <SupplierPartID>12345678</SupplierPartID>
    </ItemID>
    <ItemDetail>
      <UnitPrice>
        <Money currency="NZ">32</Money>
      </UnitPrice>
      <Description xml:lang="en">abc description</Description>
      <UnitOfMeasure>CU</UnitOfMeasure>
      <Classification domain="N/A"/>
      <ManufacturerPartID>12345678</ManufacturerPartID>
      <Extrinsic name="StockCode">12345</Extrinsic>
      <Extrinsic name="Quantity">1</Extrinsic>          
    </ItemDetail>
  </ItemOut>
  <ItemOut quantity="10" lineNumber="2" requestedDeliveryDate="2007-01-01T00:0000+10:00">
    <ItemID>
      <SupplierPartID>12345678</SupplierPartID>
    </ItemID>
    <ItemDetail>
      <UnitPrice>
        <Money currency="NZ">32</Money>
      </UnitPrice>
      <Description xml:lang="en">abc description</Description>
      <UnitOfMeasure>CU</UnitOfMeasure>
      <Classification domain="N/A"/>
      <ManufacturerPartID>12345678</ManufacturerPartID>
      <Extrinsic name="StockCode">23333</Extrinsic>
      <Extrinsic name="Quantity">1</Extrinsic>
    </ItemDetail>
  </ItemOut>
</OrderRequest>

我尝试使用此代码,但它给出了 null 或对象引用未设置错误:

XDocument xdoc = XDocument.Load(@"C:\PO.xml");
 var itemOut = (from c in xdoc.Descendants("OrderRequest")

                               select new
                               {
                                   SupplierID = c.Element("Money").Value                               ,
                                   Currency = c.Attribute("currency").Value,
                                   Money = c.Element("Money").Value,
                                   Description = c.Element("Description").Value,
                                   ManufacturerPartID = c.Element("ManufacturerPartID").Value,
                                   Extrinsic = c.Attribute("name").Value
                               });

                foreach (var element in itemOut)
                {
                    Console.WriteLine(element.SupplierID);
                }

                Console.ReadLine();

【问题讨论】:

    标签: linq-to-xml


    【解决方案1】:

    嗯,不清楚,或者您没有解释您对哪些数据感兴趣。但是,您当前选择的“OrderRequest”元素似乎没有您尝试访问的任何属性或子元素,所以我怀疑做

    var itemOut = from c in xdoc.Descendants("ItemOut")
    
                                   select new
                                   {
                                       SupplierID = c.Element("ItemID").Element("SupplierPartID").Value                               ,
                                       Currency = c.Element("ItemDetail").Element("UnitPrice").Element("Money").Attribute("currency").Value,
                                       Money = (double)c.Element("ItemDetail").Element("UnitPrice").Element("Money"),
                                       Description = c.Element("ItemDetail").Element("Description").Value,
                                       ManufacturerPartID = c.Element("ItemDetail").Element("ManufacturerPartID").Value
                                   };
    

    更接近您想要实现的目标。我不知道你想要哪个“外部”元素,所以我把它省略了。

    【讨论】:

    • 非常感谢!只是一个想法,这是获取元素值的最佳方法,即必须执行 c.Element("ItemID").Element("SupplierPartID").value 吗? .NET 无法确定我们是否执行 c.Element("SupplierPartID")?
    • 对不起,如果我们添加另外 4 个 .... 元素,代码会是什么样子,因为这就是看起来的样子(即真正的 xml 文档) .基本上 ItemOut 元素对应于行项目的数量。
    • 好吧,from c in xdoc.Descendants("ItemOut") 不会以任何方式改变,无论您有零个、一个、两个或多个“ItemOut”元素。至于c.Element("ItemID").Element("SupplierPartID"),如果你不喜欢一层一层的,你可以用c.Descendants("SupplierPartID").First()代替。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-21
    相关资源
    最近更新 更多