查找具有多个命名空间的元素
我在 C# 4.0 中使用 LINQ to XML 查找 Xml 文件中的元素时遇到问题。
以下是简化的 Xml 架构:
<?xml version="1.0" encoding="utf-8"?>
<mDoc xmlns="http://schemas.microsoft.com/taxonomy/2003/1">
<content>
<gdsPage xmlns="http://mysite.com/schemas/gdsPage/1/">
<textContainer id="C_134572">
<text id="T_399231">Content</text>
<text id="T_399232">Content</text>
</textContainer>
<textContainer id="C_134607" brands="PRMR " did="1" renderOption="" needceiling="0">
<text id="T_399268">Content</text>
</textContainer>
</gdsPage>
</content>
</mDoc>
请注意本文档中定义的两个单独的命名空间。
我在代码中定义它们如下:
XNamespace ns_mdoc = "http://schemas.microsoft.com/taxonomy/2003/1";
XNamespace ns_gds = "http://mysite.com/schemas/gdsPage/1/";
然后根据我的理解,我应该能够将命名空间添加到元素上以找到它,如下所示:
var query =
from links in
xdoc.Element(ns_gds + "linkContainer").Elements("link")
where links.Attribute("id").Value == "C_134608" || links.Attribute("id").Value == "L_233140"
select links;
这将返回 null。我尝试了许多其他访问器组合,例如 Axis search 和 Descendants:
var stuff = from links in xdoc.Descendants(ns_gds + "linkContainer")
select new {
link = links.Element(ns_gds + "link").Value
};
我还尝试使用两个名称空间,一个接着另一个。仍然为空。
我在这里缺少什么?
感谢您的浏览。
I am having troubles finding elements in an Xml file using LINQ to XML in C# 4.0.
Here is the simplified Xml Schema:
<?xml version="1.0" encoding="utf-8"?>
<mDoc xmlns="http://schemas.microsoft.com/taxonomy/2003/1">
<content>
<gdsPage xmlns="http://mysite.com/schemas/gdsPage/1/">
<textContainer id="C_134572">
<text id="T_399231">Content</text>
<text id="T_399232">Content</text>
</textContainer>
<textContainer id="C_134607" brands="PRMR " did="1" renderOption="" needceiling="0">
<text id="T_399268">Content</text>
</textContainer>
</gdsPage>
</content>
</mDoc>
Please note the two seperate namespaces defined in this doc.
I define them in my code as follows:
XNamespace ns_mdoc = "http://schemas.microsoft.com/taxonomy/2003/1";
XNamespace ns_gds = "http://mysite.com/schemas/gdsPage/1/";
Then from what I understand, I should be able to prepend the namespace onto an element to find it, like so:
var query =
from links in
xdoc.Element(ns_gds + "linkContainer").Elements("link")
where links.Attribute("id").Value == "C_134608" || links.Attribute("id").Value == "L_233140"
select links;
This returns null. I tried many other combinations of accessors like Axis search and Descendants:
var stuff = from links in xdoc.Descendants(ns_gds + "linkContainer")
select new {
link = links.Element(ns_gds + "link").Value
};
I also tried using both names spaces, one then the other. Still Null.
What am I missing here?
Thanks for taking a look.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里有几个问题:
您的 XML 不包含任何
linkContainer
或link
元素 -它们被命名为
textContainer
并且text
.您还必须使用
后代()
而不是
Elements()
如果子元素您要访问的节点不在
直接子代。
您必须在所有内容上设置命名空间
查询中的元素是
在该命名空间下 - 你是
缺少当前的命名空间
Elements("link")
部分。有了这个 XML:
这对我有用:
Several problems here:
Your XML does not contain any
linkContainer
orlink
elements -they are named
textContainer
andtext
.Also you have to use
Descendands()
and not
Elements()
if the childnodes you want to access are not
direct children.
You have to set the namespace on all
elements in your query that are
under that namespace - you are
missing the namespace currently on
the
Elements("link")
part.With this XML:
this works for me: