C# xpath获取匹配节点或其子节点的最顶层父节点
我在根目录下有这样的xml节点C# xpath获取匹配节点或其子节点的最顶层父节点,c#,xml,xpath,C#,Xml,Xpath,我在根目录下有这样的xml节点 <online.png> <createdBy>admin</createdBy> <mimeType>image/png</mimeType> // may be here in node itself <primaryType>nt:file</primaryType> <content> <firstName>Vendor</fi
<online.png>
<createdBy>admin</createdBy>
<mimeType>image/png</mimeType> // may be here in node itself
<primaryType>nt:file</primaryType>
<content>
<firstName>Vendor</firstName>
<mimeType>image/png</mimeType> // may be here in child
<city>Hyderabad</city>
</content>
</online.png>
如有任何建议,我们将不胜感激
XmlNodeList nodeImages = xmlDoc.SelectNodes("//*[contains(text(),'image/')]");
返回内容节点和gif节点。我只想要gif节点假设在您的示例中,“顶级父级”的意思是
,答案非常简单:/*[/*[contains(text(),'image')]]
-换句话说,“根元素直接包含的任何元素,它在任何深度都包含包含文本“image”的元素”.如果您想将元素放在第二层(即直接放在根目录下),其任何子元素都包含文本image/
,请使用xpath:
"/*/*[//*[contains(text(), 'image/')]]"
我在您的xml中搜索了
gif
,没有找到任何内容抱歉,它不是png。您想选择
元素吗?谢谢您的回答,但我运行此查询时得到的结果是“root”节点,而不是online.png节点。也许您应该给我们包含片段的xml。
"/*/*[//*[contains(text(), 'image/')]]"