C# .NET 中的 XPath SelectNode

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/558870/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 08:08:17  来源:igfitidea点击:

XPath SelectNodes in .NET

c#xmlxpath

提问by Gordon Thompson

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

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

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

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

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

为什么它从 B 下返回节点,而我希望发生的事情是它只从 E 下返回节点

Make sense?

有道理?

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

编辑:我如何让它只从那个节点开始返回?

采纳答案by Marc Gravell

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

简单地说:在与所选节点相同的文档中,前导 // 表示“在任何级别” 。

From the spec:

规范

  • //para selects all the para descendants of the document root and thus selects all para elements in the same document as the context node
  • .//para selects the para element descendants of the context node
  • //para 选择文档根的所有 para 后代,从而选择与上下文节点相同的文档中的所有 para 元素
  • .//para 选择上下文节点的para元素后代

回答by Jeff Yates

Specifying .//Cwill achieve what you want, otherwise, the XPath starts from the document root rather than the current node.

指定.//C将实现您想要的,否则,XPath 从文档根而不是当前节点开始。

The confusion is in the definition of //from the XPath standardas follows:

混淆//来自XPath 标准的定义如下:

// is short for /descendant-or-self::node()/. For example, //para is short for /descendant-or-self::node()/child::para and so will select any para element in the document (even a para element that is a document element will be selected by //para since the document element node is a child of the root node); div//para is short for div/descendant-or-self::node()/child::para and so will select all para descendants of div children.

// 是 /descendant-or-self::node()/ 的缩写。例如,//para 是 /descendant-or-self::node()/child::para 的缩写,因此将选择文档中的任何 para 元素(即使是作为文档元素的 para 元素也会被 / /para 因为文档元素节点是根节点的子节点);div//para 是 div/descendant-or-self::node()/child::para 的缩写,因此将选择 div 子项的所有 para 后代。

Because //is short for /descendant-or-self::node()/it starts at the document level unless you specify a node at the start.

因为///descendant-or-self::node()/它从文档级别开始的缩写,除非您在开始时指定一个节点。

回答by slf

//Cis all C nodes in the entire document

//C是整个文档中的所有C节点

/E//Cwould be only C nodes under E

/E//CE 下只有 C 个节点

/Cwould be only the root C node

/C将只是根 C 节点

See the xpath syntax reference

请参阅xpath 语法参考

回答by flq

In the XPATH Specificationyou will find under 2.5 the following statement:

XPATH 规范中,您将在 2.5 下找到以下语句:

//para selects all the para descendants of the document root and thus selects all para elements in the same document as the context node

//para 选择文档根的所有 para 后代,从而选择与上下文节点相同的文档中的所有 para 元素

i.e. the behaviour you observe is valid. You should do something like "/E//C"

即您观察到的行为是有效的。你应该做类似“/E//C”的事情