LINQ to XML 查询无知
对 LINQ to XML 非常陌生。
我有以下文档:
<?xml version="1.0"?>
<DIDemo>
<PurchaseOrders>
<PurchaseOrder date="2011-11-15" purchaseordernumber="PO-1234-1234" suppliername="The Greatest Supplier">
<LineItems>
<LineItem productcode="PRD-001" productname="A Cool Product" quantity="3" unitprice="276.99"/>
<LineItem productcode="PRD-056" productname="A Unique Product" quantity="5" unitprice="316.99"/>
</LineItems>
</PurchaseOrder>
<PurchaseOrder date="2011-11-11" purchaseordernumber="PO-1231-1231" suppliername="The Largest Supplier">
<LineItems>
<LineItem productcode="PRD-025" productname="A Useful Product" quantity="8" unitprice="96.99"/>
<LineItem productcode="PRD-014" productname="A Wonderful Product" quantity="1" unitprice="916.99"/>
</LineItems>
</PurchaseOrder>
</PurchaseOrders>
</DIDemo>
我正在尝试一个非常简单的查询来根据purchaseordernumber 属性获取采购订单。最终目标是从采购订单节点(属性和行项目)创建 ac# 对象。
我正在尝试的查询如下(我将其分成几部分以尝试查看问题出在哪里):
var document = XDocument.Load(PurchaseOrdersFilePath);
var partial1 = document.Element("PurchaseOrders"); //This returns null
var partial2 = partial1.Elements("PurchaseOrder"); //This one blows up
var final = partial2.Single(po => po.Attribute("purchaseordernumber") == criterion.PropertyValue);
如果这不起作用。查询这个的正确方法是什么?
感谢您的帮助。
Very new to LINQ to XML.
I have the following document:
<?xml version="1.0"?>
<DIDemo>
<PurchaseOrders>
<PurchaseOrder date="2011-11-15" purchaseordernumber="PO-1234-1234" suppliername="The Greatest Supplier">
<LineItems>
<LineItem productcode="PRD-001" productname="A Cool Product" quantity="3" unitprice="276.99"/>
<LineItem productcode="PRD-056" productname="A Unique Product" quantity="5" unitprice="316.99"/>
</LineItems>
</PurchaseOrder>
<PurchaseOrder date="2011-11-11" purchaseordernumber="PO-1231-1231" suppliername="The Largest Supplier">
<LineItems>
<LineItem productcode="PRD-025" productname="A Useful Product" quantity="8" unitprice="96.99"/>
<LineItem productcode="PRD-014" productname="A Wonderful Product" quantity="1" unitprice="916.99"/>
</LineItems>
</PurchaseOrder>
</PurchaseOrders>
</DIDemo>
And I'm trying a very simple query to get the Purchase Order based on the purchaseordernumber attribute. The end goal being to create a c# object out of the Purchase Order node (attributes and line items).
The query I'm trying is the following (I broke it up in pieces to try and see where's the issue):
var document = XDocument.Load(PurchaseOrdersFilePath);
var partial1 = document.Element("PurchaseOrders"); //This returns null
var partial2 = partial1.Elements("PurchaseOrder"); //This one blows up
var final = partial2.Single(po => po.Attribute("purchaseordernumber") == criterion.PropertyValue);
If this doesn't work. What would be the correct way to query this?
Thanks for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您必须添加文档根作为基础:
或者,在您的情况下可能更容易,您可以使用
Descendants()
查询 XML 文档中所有级别的所有PurchaseOrder
节点:当然你可以将其结合到最终结果中:
You have to add the document root as base:
Alternatively and probably easier in your case you can use
Descendants()
to query for allPurchaseOrder
nodes on all levels in the XML document:And of course you can combine this in the final result:
要在文档中的任何位置查找元素,您可以使用“后代”。
相关:
To find elements anywhere in the document you can use
Descendants
.Related:
那么,文件的最外层元素不是
而是
。一旦partial1
为空,取消引用它就会导致你的程序崩溃。Well, the outermost element of the file is not
<PurchaseOrders>
but<DIDemo>
. And oncepartial1
is null, dereferencing it is going to cause your program to blow up.