title: Powershell: select XML node value into variable by element index
body: <p>I have this PowerShell script which I use to parse through different aspects of an XML-file. Therefore, I Would like to retrieve the value of a specific XML-node and save it into a variable. This node I would like to select, exists multiple times in the XML-file, so I need to be able to mention the specific number of the node I want to retrieve. </p>
  
  <p>XML-file: </p>
  
  <pre><code>&lt;lvl1&gt;
      &lt;lvl2&gt;
          &lt;lvl3&gt;
              &lt;lvl4&gt;test1&lt;/lvl4&gt;
              &lt;lvl4&gt;test2&lt;/lvl4&gt;
              &lt;lvl4&gt;test3&lt;/lvl4&gt;
          &lt;/lvl3&gt;
      &lt;/lvl2&gt;
  &lt;/lvl1&gt;
  </code></pre>
  
  <p>Powershell scripts I've tried:</p>
  
  <h1>1 returning nothing and no errors</h1>
  
  <pre><code>[xml] $response = get-content "FilePath\Resp.xml"
  [string] $Var = $response.SelectSingleNode('lvl1/lvl2/lvl3/lvl4[2]').InnerText
  </code></pre>
  
  <h1>2 returning error: "cannot index into a null array."</h1>
  
  <pre><code>[xml] $response = get-content "FilePath\Resp.xml"
  $Var = $response.lvl1.lvl2.lvl3.lvl4[2].'#text'
  </code></pre>