当元素/根具有命名空间作为属性时,查询 XDocument 查询不起作用

发布于 2024-11-26 22:27:40 字数 1116 浏览 1 评论 0原文

问题是,如果节点包含名称空间/属性,我无法获得任何结果。这是代码:

Dim xmlFromDisk = XDocument.Load("customers.xml")
Dim ukCustomers = <ukCustomers>
                    <%= From cust In xmlFromDisk...<Customer> _
                    Where cust.<Country>.Value = "UK" _
                    Select cust %>
                  </ukCustomers>

当我有以下customers.xml 时,查询有效:

<?xml version="1.0" encoding="utf-8"?>
<Customers>
  <Customer>
    <CustomerID>ALFKI</CustomerID>
    <CompanyName>Alfreds Futterkiste</CompanyName>
    <Country>UK</Country>
  </Customer>
  </Customers>

当我有以下customers.xml 时,查询不起作用:

    <?xml version="1.0" encoding="utf-8"?>
<Customers xmlns="http://tempuri.org/">
  <Customer>
    <CustomerID>ALFKI</CustomerID>
    <CompanyName>Alfreds Futterkiste</CompanyName>
    <Country>UK</Country>
  </Customer>
  </Customers>

唯一的区别是命名空间 xmlns="http://tempuri.org/"客户元素。

The problem is that I can not get any results if the node contains namespace/attribute. This is the code:

Dim xmlFromDisk = XDocument.Load("customers.xml")
Dim ukCustomers = <ukCustomers>
                    <%= From cust In xmlFromDisk...<Customer> _
                    Where cust.<Country>.Value = "UK" _
                    Select cust %>
                  </ukCustomers>

When I have the following customers.xml the query works:

<?xml version="1.0" encoding="utf-8"?>
<Customers>
  <Customer>
    <CustomerID>ALFKI</CustomerID>
    <CompanyName>Alfreds Futterkiste</CompanyName>
    <Country>UK</Country>
  </Customer>
  </Customers>

When I have the following customers.xml the query DOES NOT work:

    <?xml version="1.0" encoding="utf-8"?>
<Customers xmlns="http://tempuri.org/">
  <Customer>
    <CustomerID>ALFKI</CustomerID>
    <CompanyName>Alfreds Futterkiste</CompanyName>
    <Country>UK</Country>
  </Customer>
  </Customers>

The only difference is the namespace xmlns="http://tempuri.org/" in the Customers element.

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

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

发布评论

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

评论(1

黒涩兲箜 2024-12-03 22:27:40

当然,所以您还需要指定名称空间。 xmlns="..." 表示任何不合格的后代元素的默认命名空间。

我不知道如何在 VB 中的 XML 文字中执行此操作,但在 C# 中,您只需编写:

XNamespace ns = "http://tempuri.org/";
var ukCustomers = doc.Root
                     .Elements(ns + "Customer")
                     .Where(x => (string) x.Element(ns + "Country") == "UK");

编辑:这是由 Reflector 显示的等效 VB 代码,并由我修改了一下:

Dim ns As XNamespace = "http://tempuri.org/"
Dim ukCustomers =
   (From x In doc.Root.Elements(DirectCast((ns + "Customer"), XName))
    Where (CStr(x.Element(ns + "Country")) = "UK")
    Select x)

Sure, so you need to specify the namespace as well. xmlns="..." indicates the default namespace for any unqualified descendant elements.

I don't know how you'd do it in an XML literal in VB, but in C# you'd just write:

XNamespace ns = "http://tempuri.org/";
var ukCustomers = doc.Root
                     .Elements(ns + "Customer")
                     .Where(x => (string) x.Element(ns + "Country") == "UK");

EDIT: This is the equivalent VB code as shown by Reflector and hacked around a bit by me:

Dim ns As XNamespace = "http://tempuri.org/"
Dim ukCustomers =
   (From x In doc.Root.Elements(DirectCast((ns + "Customer"), XName))
    Where (CStr(x.Element(ns + "Country")) = "UK")
    Select x)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文