Search code examples
c#xmlxpathxmldocument.net-6.0

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


I have the following 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>

I would like to get a list of the Action nodes where stage/loginhibit.onsuccess is "false" or null. The XPath in this statement:

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

will give me the correct results on xpather.com, but not in .NET 6 using VSCode, which returns an empty list.

Thanks in advance!


Solution

  • You can do this more simply with

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