title: Powershell & XML: How to count specific elements for each node
body: <p>Here is another one for my server for the PC Game - Space Engineers. The answer to this seems like it should be simple but it has me stuck as i can not find the right way to call this information.</p>
  
  <p>What I would like to do is get the count of how many times a specific element appears in each node. I have this partially working but not exactly what I want. </p>
  
  <p>Here is what I have so far:</p>
  
  <p>An excerpt from the XML (nodes folded except for target node)</p>
  
  <pre><code>&lt;MyObjectBuilder_Sector xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
    &lt;Position&gt;
    &lt;SectorEvents&gt;
    &lt;AppVersion&gt;
    &lt;SectorObjects&gt;
       &lt;MyObjectBuilder_EntityBase xsi:type="MyObjectBuilder_CubeGrid"&gt;
          &lt;CubeBlocks&gt;
             &lt;MyObjectBuilder_CubeBlock xsi:type="MyObjectBuilder_Reactor"&gt;
             &lt;MyObjectBuilder_CubeBlock xsi:type="MyObjectBuilder_Thrust"&gt;
             &lt;MyObjectBuilder_CubeBlock xsi:type="MyObjectBuilder_Drill"&gt;
             &lt;MyObjectBuilder_CubeBlock xsi:type="MyObjectBuilder_Drill"&gt;
                &lt;SubtypeName&gt;SmallBlockDrill&lt;/SubtypeName&gt;
                &lt;EntityId&gt;72280681079646079&lt;/EntityId&gt;
                &lt;Min x="1" y="1" z="-7" /&gt;
                &lt;BlockOrientation Forward="Forward" Up="Left" /&gt;
                &lt;ColorMaskHSV x="0" y="-1" z="0" /&gt;
                &lt;Owner&gt;144256542526969420&lt;/Owner&gt;
                &lt;ShareMode&gt;None&lt;/ShareMode&gt;
                &lt;ShowOnHUD&gt;false&lt;/ShowOnHUD&gt;
                &lt;Enabled&gt;false&lt;/Enabled&gt;
                &lt;Inventory&gt;
                  &lt;Items /&gt;
                  &lt;nextItemId&gt;0&lt;/nextItemId&gt;
                &lt;/Inventory&gt;
             &lt;/MyObjectBuilder_CubeBlock&gt;
  </code></pre>
  
  <p>and my powershell code that gives me back the number of cube blocks per </p>
  
  <pre><code>&lt;MyObjectBuilder_EntityBase xsi:type="MyObjectBuilder_CubeGrid"&gt;
  </code></pre>
  
  <p>cube grid.</p>
  
  <pre><code>$filePath = 'F:\DedicatedServer\DataDir\SE Survival 2\Saves\VPS RC 1\SANDBOX_0_0_0_.sbs'
  [xml]$myXML = Get-Content $filePath
  $ns = New-Object System.Xml.XmlNamespaceManager($myXML.NameTable)
  $ns.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance")
  
  $infos = $myXML.SelectNodes("//SectorObjects/MyObjectBuilder_EntityBase[(@xsi:type='MyObjectBuilder_CubeGrid')]" ,$ns)
  
  foreach ($info in $infos ){
  
          $info.CubeBlocks.MyObjectBuilder_CubeBlock.count 
  }
  </code></pre>
  
  <p>So I am trying to get this to feed back the number of small drills per cube grid. seen here in the XML</p>
  
  <pre><code>&lt;MyObjectBuilder_CubeBlock xsi:type="MyObjectBuilder_Drill"&gt;
        &lt;SubtypeName&gt;SmallBlockDrill&lt;/SubtypeName&gt;
  </code></pre>
  
  <p>I feel Like this is close to solved but its returning the same number for every cube grid so that can not be correct.</p>
  
  <pre><code>$info = $info.SelectNodes("//CubeBlocks/MyObjectBuilder_CubeBlock[(@xsi:type='MyObjectBuilder_Drill')]/SubtypeName['SmallBlockDrill']" ,$ns).InnerText
  $info.count
  </code></pre>
  
  <p>Results below. I believe this is the total number of both large and small drills in the world just repeating for every grid it finds. feels close but i'm betting the subtype value selection is not working as I want it to.</p>
  
  <pre><code>490
  490
  490
  490
  490
  490
  490
  490
  </code></pre>