c# - 仅选择直系后代

标签 c# xpath

我有这样的结构:

<root>
  <properties>
    <property name="test">
      <value>X</value>
    </property>
  </properties>
  <things>
    <thing>
      <properties>
        <property name="test">
          <value>Y</value>
        </property>
      </properties>
    </thing>
  </things>
</root>

如果使用 <root> 运行,是否有一个 XPath 表达式选择值为 X 的测试属性作为 root,如果以 thing 运行,则只有值为 Y 的那个作为根?

我认为 /properties/property[@name='test']会要求它是一个直接的 child ,但这似乎没有任何返回。如果我删除斜杠,我会同时得到 property元素(我使用的是 C#,带有 XElement root = ...; root.XPathSelectElements(xpathexpression); )。

最佳答案

我认为你的意思是 Property 而不是 Properties。试试 ./properties/property[@name='test']

关于c# - 仅选择直系后代,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9278091/

相关文章:

c# - TestCleanup() 实现

c# - 如何删除xamarin形式的操作栏?

java - 返回 JOOX 中的第一级标签

python - 从 xpath 中删除信息?

XMLStarlet、XPath - 如何根据节点删除属性

xml - 从包含 CDATA 标记的 SOAPUI 中的 XML 响应中检索节点值

c# - 改变进度条的大小

c# - 无法在 C# 中将域解析为 IP?

c# - 比较对象,忽略成员的 "order"

soap - 使用 Xpath 从 SOAP 消息中获取值