更改 .net (C#) 中 XmlDocument 的 XPath 根目录?

发布于 2024-10-12 15:19:56 字数 1339 浏览 2 评论 0原文

当通过例如 XPath 方法 SelectSingleNode 从 XmlDocument 中进行选择时,我们得到一个由第一个匹配节点组成的 XmlNode,我们将其称为。如果我们对进行进一步选择,那么人们可能会认为现在的 XPath-root 就是这个节点,但这是不正确的,根仍然与原始 XmlDocument 中的根相同。这是一个例子:

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 技术交流群。

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

发布评论

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

评论(2

韬韬不绝 2024-10-19 15:19:56

由于您从 Root/SubElement 中进行选择,请尝试以下操作:

XmlNode xnElm = xnSubRoot.SelectSingleNode("elm"); 

它将返回当前节点的第一个 elm 子节点。

编辑(根据注释中提供的附加信息):

在这种特定情况下,您将从 WebService 调用中获取 XmlNode (这是您的数据节点)。该 XmlNode 上的所有 XPath 请求都将与其相关。

我建议您修改所有 XPath 以使用像 webServiceNode.SelectSingleNode("SubElement/SubSubElement"); 这样的选择器。没有理由在此处指定绝对 XPaths 查询。

Since your selecting from the Root/SubElement Try this:

XmlNode xnElm = xnSubRoot.SelectSingleNode("elm"); 

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 that XmlNode 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.

油焖大侠 2024-10-19 15:19:56

这有效:

XmlNode xnSubRoot = xd.SelectSingleNode("/root/subroot");
XmlNode xnElm = xnSubRoot.SelectSingleNode("elm");

这也是如此:

XmlNode xnRoot = xd.SelectSingleNode("/root");
XmlNode xnElm = xnRoot.SelectSingleNode("subroot/elm");

This works:

XmlNode xnSubRoot = xd.SelectSingleNode("/root/subroot");
XmlNode xnElm = xnSubRoot.SelectSingleNode("elm");

And so does this:

XmlNode xnRoot = xd.SelectSingleNode("/root");
XmlNode xnElm = xnRoot.SelectSingleNode("subroot/elm");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文