title: How to extract the value of xml node to a variable in powershell?
body: <p>I am working to build a Power shell script that reads a XML file and extracts the values from the nodes to make further calculations. How could I extract the node values to variables?</p>
  
  <p>I have written code using XMLDOC in PS to read the file and extract the values in Power shell. I am able to get the attribute value of a single node. I am not able to extract the node value which has attributes too and there are multiple occurrences of the node.</p>
  
  <blockquote>
    <p>Test.xml</p>
  </blockquote>
  
  <pre><code>&lt;abc&gt;
    &lt;body&gt;
      &lt;Rows Count="02"&gt;
        &lt;Row&gt;
          &lt;a ID="1"&gt;Name1&lt;/a&gt;
          &lt;a ID="2"&gt;Naresh&lt;/a&gt;
          &lt;a ID="3"&gt;Arvind&lt;/a&gt;
        &lt;/Row&gt;
        &lt;Row&gt;
          &lt;a ID="1"&gt;Name2&lt;/a&gt;
          &lt;a ID="2"&gt;Sathish&lt;/a&gt;
          &lt;a ID="3"&gt;Kannan&lt;/a&gt;
        &lt;/Row&gt;
      &lt;/Rows&gt;
    &lt;/body&gt;
    &lt;abc&gt;
  </code></pre>
  
  <blockquote>
    <p>Code</p>
  </blockquote>
  
  <pre><code>   [xml]$XmlDocument = Get-Content E:\Desktop\Test.xml 
     $nodes = $XmlDocument.SelectNodes('//Rows') 
     $A = $nodes.GetAttribute("count")  
  
     if ($A -eq '02')
      {
       $B = $XmlDocument.abc.Body.Rows.ROW.a | Where-Object {$_.ID -eq '2'}
      }
  </code></pre>
  
  <blockquote>
    <p>Expected Result</p>
  </blockquote>
  
  <p>Variable A should get "02"(only value) and "Naresh","Sathish" (only values) should be stored in 2 different variables</p>
  
  <blockquote>
    <p>Actual Result</p>
  </blockquote>
  
  <p>Variable A gets "02" - it is working
  both "Naresh" and "Sathish" are stored in Variable B but stored with the IDs and headers.</p>