如何:搜索XML子节点

Tyg*_*ash 1 c# xml xpath

给出一块Xml就像下面的一样.如何编写XPATH查询以获取'leaf2'子项的值,其中'key'值具有特定值(比如2)

我在C#.NET工作.目前我只是想使用SelectNodes获取密钥的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>
Run Code Online (Sandbox Code Playgroud)

Wel*_*bog 8

你要:

/root/child[anotherChild/key = '2']/leaf2
Run Code Online (Sandbox Code Playgroud)

这就是说,"获取命名的元素leaf2,其父级是child祖父母root,其祖父母是在哪里child被子级命名anotherChild,其子级名为key其值为的子级2."