给定一段 Xml,如下所示。我将如何编写 XPATH 查询来获取 'leaf2' 子项的值,其中 'key' 值具有特定值(比如 2)
我在 C# .NET 中工作。目前,我只是在考虑使用 SelectNodes 获取 key 的 Xpath,找到正确的值,然后导航回 leaf2。
<root>
<child>
<anotherChild>
<key>1</key>
</anotherChild>
<leaf1>X</leaf1>
<leaf2>Y</leaf2>
<leaf3></leaf3>
</child>
<child>
<anotherChild>
<key>2</key>
</anotherChild>
<leaf1>A</leaf1>
<leaf2>B</leaf2>
<leaf3></leaf3>
</child>
</root>
最佳答案
你想要:
/root/child[anotherChild/key = '2']/leaf2
这就是说,“获取名为 leaf2
的元素,其父级为 child
,其祖父级为 root
,其中 child
正在被其名为 anotherChild
的子项过滤,其子项名为 key
,其值为 2
。”
关于c# - 如何: seaching XML child nodes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/721598/