LINQ to XML-读取XML文档 [英] LINQ to XML - Reading XML Document

查看:55
本文介绍了LINQ to XML-读取XML文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Could someone please help me how to read the XML Document below using Linq to XML please?

<?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或对象引用未设置错误:

I tried with this code, but it's giving either null or object reference not set error:

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();

推荐答案

还不清楚,或者您还没有解释您感兴趣的数据.但是,当前选择的"OrderRequest"元素似乎并不具有您尝试访问的任何属性或子元素,所以我怀疑这样做

Well it is not clear or rather you have not explained which data you are interested in. However the "OrderRequest" element you currently select does not seem the one having any of the attributes or child elements you are trying to access so I suspect doing

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
                               };

更接近您想要实现的目标.我无法弄清楚您想要哪个外部"元素,因此我将其遗漏了.

is closer to what you want to achieve. I couldn't figure which "Extrinsic" element you want so I left that out.

这篇关于LINQ to XML-读取XML文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆