C# - 用于查找为空或具有特定属性值的子节点的 XPath

C# - XPath to find Subnodes that are null, OR have a specific Attribute value

我有以下 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.onsuccess 为“false”或 null 的 Action 节点的列表。此语句中的 XPath:

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

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

提前致谢!

前导正斜杠丢失:

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

总的来说,还是用LINQ比较好XMLAPI。它自 2007 年起在 .NET Framework 中可用。

您可以使用

更简单地完成此操作
/process/stage[@type='Action'][not(loginhibit/@onsuccess='true')]