当元素/根具有命名空间作为属性时,查询 XDocument 查询不起作用
问题是,如果节点包含名称空间/属性,我无法获得任何结果。这是代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当然,所以您还需要指定名称空间。
xmlns="..."
表示任何不合格的后代元素的默认命名空间。我不知道如何在 VB 中的 XML 文字中执行此操作,但在 C# 中,您只需编写:
编辑:这是由 Reflector 显示的等效 VB 代码,并由我修改了一下:
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:
EDIT: This is the equivalent VB code as shown by Reflector and hacked around a bit by me: