首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >LINQ to XML -读取XML文档

LINQ to XML -读取XML文档
EN

Stack Overflow用户
提问于 2011-05-29 19:37:41
回答 1查看 3.3K关注 0票数 1

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

代码语言:javascript
复制
<?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或对象引用未设置错误:

代码语言:javascript
复制
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();
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-05-29 20:07:48

嗯,不清楚,或者更确切地说,你还没有解释你对哪些数据感兴趣。但是,您当前选择的"OrderRequest“元素似乎没有您试图访问的任何属性或子元素,所以我怀疑是这样做的

代码语言:javascript
复制
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
                               };

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6167541

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档