PowerShell - Fetch all the XML Nodes with based on an Attribute Value

263 Views Asked by At

I am trying to fetch all the XML nodes, where Attribute "Tag" value is "Fetch" with a XPath, without looping much using PowerShell 5.1. Please help.

[xml] $XMLValue=@'
  <Root>
     <Child>
        <Parameter Tag="Fetch"><![CDATA["Data"]]></Parameter>
        <Parameter Tag="DontFetch"><![CDATA["Data"]]></Parameter>
     </Child>
     <Child>
        <Test>
           <Node Tag="Fetch"><![CDATA["Data"]]></Node>
           <Node Tag="DontFetch"><![CDATA["Data"]]></Node>
        </Test>
     </Child>
     <Child>
     <Test>
        <Test1>
           <Node1 Tag="Fetch"><![CDATA["Data"]]></Node1>
           <Node2 Tag="DontFetch"><![CDATA["Data"]]></Node2>
        </Test1>
     </Test>     
    </Child>
   </Root>
'@

I tried to loop in each element of the XML using XML "SelectNodes" method and recursing, but it is too much to manage the depth. So is there an Xpath for this type if requirement, where I can filter the Nodes based on an Attribute value.

Output shoud be below 3 Nodes

<Parameter Tag="Fetch"><![CDATA["Data"]]></Parameter>
<Node Tag="Fetch"><![CDATA["Data"]]></Node>
<Node1 Tag="Fetch"><![CDATA["Data"]]></Node1>
0

There are 0 best solutions below