LINQ to XML 查询无知

发布于 2024-12-15 18:13:49 字数 1595 浏览 0 评论 0原文

对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

檐上三寸雪 2024-12-22 18:13:49

您必须添加文档根作为基础:

var partial1 = document.Root.Element("PurchaseOrders");

或者,在您的情况下可能更容易,您可以使用 Descendants() 查询 XML 文档中所有级别的所有 PurchaseOrder 节点:

var orders = document.Descendants("PurchaseOrder"); 

当然你可以将其结合到最终结果中:

var order = document.Descendants("PurchaseOrder")
                    .Single(po => (string)po.Attribute("purchaseordernumber") == criterion.PropertyValue);

You have to add the document root as base:

var partial1 = document.Root.Element("PurchaseOrders");

Alternatively and probably easier in your case you can use Descendants() to query for all PurchaseOrder nodes on all levels in the XML document:

var orders = document.Descendants("PurchaseOrder"); 

And of course you can combine this in the final result:

var order = document.Descendants("PurchaseOrder")
                    .Single(po => (string)po.Attribute("purchaseordernumber") == criterion.PropertyValue);
清眉祭 2024-12-22 18:13:49

要在文档中的任何位置查找元素,您可以使用“后代”。

相关:

To find elements anywhere in the document you can use Descendants.

Related:

一个人的旅程 2024-12-22 18:13:49

那么,文件的最外层元素不是 而是 。一旦 partial1 为空,取消引用它就会导致你的程序崩溃。

Well, the outermost element of the file is not <PurchaseOrders> but <DIDemo>. And once partial1 is null, dereferencing it is going to cause your program to blow up.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文