更改 .net (C#) 中 XmlDocument 的 XPath 根目录?
当通过例如 XPath 方法 SelectSingleNode 从 XmlDocument 中进行选择时,我们得到一个由第一个匹配节点组成的 XmlNode,我们将其称为
XmlDocument xd = new XmlDocument();
xd.LoadXml(@"<root>
<subroot>
<elm>test1</elm>
<elm>test2</elm>
<elm>test3</elm>
</subroot>
</root>");
XmlNode xnSubRoot = xd.SelectSingleNode("/root/subroot");
//This is the XPath I want to be able to use, but it returns null.
XmlNode xnElm = xnSubRoot.SelectSingleNode("/subroot/elm");
//This works, but the XPath-root is the same as in the original document.
xnElm = xnSubRoot.SelectSingleNode("/root/subroot/elm");
有没有办法“修复” xnSubRoot 的根,以便我可以使用我想要的 XPath?我的问题的原因是因为我有一个案例,我正在调用一个返回 XmlNode 的 Web 服务,其中 OuterXml 属性显示“/Data/SubElement/...”等结构,但是在运行 XPath 时“ /Data”则返回 null,只有“/SubElement”有效,即 XPath 根似乎比实际文档根低一级。
我确信对此有一个完全合理的解释,或者我错过了一些重要的东西。然而,即使我读过 http://msdn.microsoft.com/en-us/library/d271ytdx(VS.80).aspx。
注意,我确实意识到可以使用 XPath“//subroot/elm”,但随后我也可能会在 XML 结构中进一步获取其他元素。
When selecting from an XmlDocument by e.g. the XPath-method SelectSingleNode we get an XmlNode that consist of the first matching node, lets call it <node1>. If we do further selection on <node1> then one might expect that the XPath-root now is this node, but this is incorrect, the root is still the same as in the original XmlDocument. Here's an example:
XmlDocument xd = new XmlDocument();
xd.LoadXml(@"<root>
<subroot>
<elm>test1</elm>
<elm>test2</elm>
<elm>test3</elm>
</subroot>
</root>");
XmlNode xnSubRoot = xd.SelectSingleNode("/root/subroot");
//This is the XPath I want to be able to use, but it returns null.
XmlNode xnElm = xnSubRoot.SelectSingleNode("/subroot/elm");
//This works, but the XPath-root is the same as in the original document.
xnElm = xnSubRoot.SelectSingleNode("/root/subroot/elm");
Is there any way to "fix" the root of xnSubRoot so that I can use the XPath I want? The reason for my question is because I have a case where I'm calling a webservice that returns an XmlNode where the OuterXml-property shows a structure of "/Data/SubElement/..." and so on, but when running XPath "/Data" then null is returned, only "/SubElement" works, i.e. the XPath-root seems to be one level lower than the actual document-root.
I'm sure there is a perfectly reasonable explanation for this, or that I'm missing something vital. However I really can't seem to find anything, even though I've read http://msdn.microsoft.com/en-us/library/d271ytdx(VS.80).aspx.
N.B. I do realize that it would be possible to use the XPath "//subroot/elm", but then I might also get other elements further down in the XML structure.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于您从
Root/SubElement
中进行选择,请尝试以下操作:它将返回当前节点的第一个
elm
子节点。编辑(根据注释中提供的附加信息):
在这种特定情况下,您将从 WebService 调用中获取
XmlNode
(这是您的数据节点)。该XmlNode
上的所有 XPath 请求都将与其相关。我建议您修改所有 XPath 以使用像
webServiceNode.SelectSingleNode("SubElement/SubSubElement");
这样的选择器。没有理由在此处指定绝对 XPaths 查询。Since your selecting from the
Root/SubElement
Try this:It will return the first child
elm
node of the current node.Edit (from additionals informations provided in comments):
In this specific case, you are obtaning a
XmlNode
(which is your Data node) from a WebService call. All XPath requests on thatXmlNode
will be relative to it.I would suggest that you modify all your XPaths to use a selector like
webServiceNode.SelectSingleNode("SubElement/SubSubElement");
. There is no reason to specify absolute XPaths queries here.这有效:
这也是如此:
This works:
And so does this: