C#-XPath查找空的子节点,或者具有特定的Attribute值



我有以下XML:

<process>
<stage stageid="f163a7e1-1343-4864-9f3f-b1d885445d15" name="Start" type="Start">
<subsheetid>1c12d9f9-9d2c-43d0-b267-ae6eb1d4b589</subsheetid>
</stage>

<stage stageid="8ca8728e-c30b-4748-87a8-4c7214066d8c" name="End" type="End">
<subsheetid>1c12d9f9-9d2c-43d0-b267-ae6eb1d4b589</subsheetid>
</stage>

<stage stageid="9a7a7a97-30d9-455c-aa7c-daf954e8f9f8" name="Kill Chrome" type="Action">
<subsheetid>1c12d9f9-9d2c-43d0-b267-ae6eb1d4b589</subsheetid>
<loginhibit onsuccess="false" />
</stage>

<stage stageid="0b76701a-3c47-46c2-af22-8be4d10c9611" name="Kill Outlook" type="Action">
<subsheetid>1c12d9f9-9d2c-43d0-b267-ae6eb1d4b589</subsheetid>
<loginhibit onsuccess="true" />
</stage>
<stage stageid="7419f58d-a8f4-47af-8feb-b25a10d7824e" name="File Exists?" type="Action">
<subsheetid>add10b41-dbad-4295-8a98-a5553064e9a1</subsheetid>
</stage>
</process>

我想得到一个操作节点的列表,其中stage/loginhibit.onsaccess是";false";或为空。此语句中的XPath:

XmlPathNodeList stages = _xmldoc.SelectNodes("process/stage[@type='Action'][loginhibit[@onsuccess='false']or(not(loginhibit))]");

会在xpather.com上给我正确的结果,但不会在中。NET 6使用VSCode,它返回一个空列表。

提前感谢!

使用可以更简单地做到这一点

/process/stage[@type='Action'][not(loginhibit/@onsuccess='true')]

缺少前导正斜杠:

/process/stage[@type='Action']
[loginhibit[@onsuccess='false']or(not(loginhibit))]

总体而言,最好使用LINQ to XML API。中提供。NET框架。

相关内容

最新更新