.NET中的XPath SelectNodes [英] XPath SelectNodes in .NET

查看:43
本文介绍了.NET中的XPath SelectNodes的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<Document>
  <A> 
    <B> 
      <C></C>
    </B>
  </A>
  <E>
   <F>
    <C></C>
   </F>
   <G>
    <C></C>
  </G>
 </E>
</Document>

如果我将上述XML加载到XmlDocument中,并使用XPath查询//C在A上执行SelectSingleNode

If i load the above XML into an XmlDocument and do a SelectSingleNode on A using the XPath query //C


XmlNode oNode = oDocument.SelectSingleNode("E");
XmlNodeList oNodeList = oNode.SelectNodes("//C");

为什么我期望发生的事情是它仅从E下返回节点,为什么它从B下返回节点?

why does it return nodes from Under B when what I would expect to happen would that it only return nodes from under E

有道理吗?

我如何使它仅从该节点开始返回?

Edit : How would i make it only return from that node onwards?

推荐答案

简单来说:开头//表示与所选节点相同的 document 中的任何级别".

Simply: a leading // means "at any level" in the same document as the selected node.

来自规范:

  • //para选择文档根目录的所有para后代,并因此选择同一文档中的所有para元素作为上下文节点
  • .//para选择上下文节点的para元素后代

这篇关于.NET中的XPath SelectNodes的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆