c# - 如何: seaching XML child nodes

标签 c# xml xpath

给定一段 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/

相关文章:

python - 将 XML 子元素解析回字符串

c# - 在 XML 反序列化中使用继承

c# - 在另一个表单上更改文本框的文本

c# - 如何使用MVVM以编程方式更改ComboBox SelectedValue

c# - .net 核心中的 MissingManifestResourceException

c++ - 如何循环 n 次,在 xml 文件中每个循环一个级别

xml - 如何通过 Ruby 中的 RELAX NG 验证 XML?

php - 使用 XPath 获取最高值节点

xml - 使用xmlstarlet/xpath获取所有子名称

c# - 同时接收 PictureBox 和 DataGridViewImageCell 的函数