Linq to XML - 查询不返回任何内容

发布于 2024-09-11 21:46:33 字数 1483 浏览 4 评论 0原文

我有一个 XML 文件,当我运行 LINQ 查询时,for 循环中不会返回任何内容。

<?xml version="1.0" encoding="utf-8"?>
<ReadOrderResponseType xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 <Status xmlns="urn:mysite:apis">Success</Status>
 <TimeStamp xmlns="urn:mysite:apis">2010-07-25T15:26:49.1150235Z</TimeStamp>
 <StoreUrl xmlns="urn:mysite:apis">http://XYZ.COM</StoreUrl>
 <PageResponse xmlns="urn:mysite:apis">
  <Page>1</Page>
  <Size>10</Size>
  <TotalSize>18033</TotalSize>
  <HasMore>true</HasMore>
 </PageResponse>
 <OrderList OrderId="20147184" OrderNumber="20100560" xmlns="urn:mysite:apis">
  <Archived>false</Archived>
  <CreateDate>2010-07-25T01:37:04.067Z</CreateDate>
</OrderList>
</ReadOrderResponseType>

VB.NET:

Dim document As XDocument = XDocument.Parse(myOrders.xml)
                Dim query = From O In document.Root.Elements("OrderList") _
                 Select New With _
                 { _
                    .Archived= O.Element("Archived").Value, _
                    .Status = O.Element("CreateDate").Value _
                 }

                For Each Order In query
                    Console.WriteLine(Order.Archived)
                    Console.ReadLine()
                Next

它甚至不进入 for 循环。我做错了什么?

I have a XML file on which when I run a LINQ query returns nothing in the for loop.

<?xml version="1.0" encoding="utf-8"?>
<ReadOrderResponseType xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 <Status xmlns="urn:mysite:apis">Success</Status>
 <TimeStamp xmlns="urn:mysite:apis">2010-07-25T15:26:49.1150235Z</TimeStamp>
 <StoreUrl xmlns="urn:mysite:apis">http://XYZ.COM</StoreUrl>
 <PageResponse xmlns="urn:mysite:apis">
  <Page>1</Page>
  <Size>10</Size>
  <TotalSize>18033</TotalSize>
  <HasMore>true</HasMore>
 </PageResponse>
 <OrderList OrderId="20147184" OrderNumber="20100560" xmlns="urn:mysite:apis">
  <Archived>false</Archived>
  <CreateDate>2010-07-25T01:37:04.067Z</CreateDate>
</OrderList>
</ReadOrderResponseType>

VB.NET:

Dim document As XDocument = XDocument.Parse(myOrders.xml)
                Dim query = From O In document.Root.Elements("OrderList") _
                 Select New With _
                 { _
                    .Archived= O.Element("Archived").Value, _
                    .Status = O.Element("CreateDate").Value _
                 }

                For Each Order In query
                    Console.WriteLine(Order.Archived)
                    Console.ReadLine()
                Next

It does not even go inside the for loop. What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

浅笑依然 2024-09-18 21:46:33

您需要一个命名空间限定符。尝试类似的方法:

    Dim document As XDocument = XDocument.Load("XMLFile1.xml")
    Dim apins As XNamespace = "urn:mysite:apis"
    Dim query = From O In document.Root.Elements(apins + "OrderList") _
     Select New With _
     { _
        .Archived = O.Element(apins + "Archived").Value, _
        .Status = O.Element(apins + "CreateDate").Value _
     }

    For Each Order In query
        Console.WriteLine(Order.Archived)
        Console.ReadLine()
    Next

You need a namespace qualifier. Try something like:

    Dim document As XDocument = XDocument.Load("XMLFile1.xml")
    Dim apins As XNamespace = "urn:mysite:apis"
    Dim query = From O In document.Root.Elements(apins + "OrderList") _
     Select New With _
     { _
        .Archived = O.Element(apins + "Archived").Value, _
        .Status = O.Element(apins + "CreateDate").Value _
     }

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