title: Parsing XML Code Metrics Report using Powershell
body: <p>I'm hoping I can get a little help over this scripting hump. I'm trying to parse this XML. Currently it's being done via C#, but I'm trying to re-write it in powershell. </p>
  
  <pre><code>&lt;CodeMetricsReport Version="10.0"&gt;
    &lt;Targets&gt;
      &lt;Target Name="C:\Builds\APP\APP_v1.0.0\Data.dll"&gt;
        &lt;Modules&gt;
          &lt;Module Name="Data.dll" AssemblyVersion="1.0.0" FileVersion="1.0.0"&gt;
            &lt;Metrics&gt;
              &lt;Metric Name="MaintainabilityIndex" Value="84" /&gt;
              &lt;Metric Name="CyclomaticComplexity" Value="39" /&gt;
              &lt;Metric Name="ClassCoupling" Value="14" /&gt;
              &lt;Metric Name="DepthOfInheritance" Value="1" /&gt;
              &lt;Metric Name="LinesOfCode" Value="101" /&gt;
            &lt;/Metrics&gt;
          &lt;/Module&gt;
        &lt;/Modules&gt;
      &lt;/Target&gt;
    &lt;/Targets&gt;      
  &lt;/CodeMetricsReport&gt;
  </code></pre>
  
  <p>This is a snippet of the C# code that I'm currently using to parse the above XML.</p>
  
  <pre><code>XmlDocument xmlDoc = new XmlDocument();
  xmlDoc.Load(MetricsFileName);
  
  XmlNodeList modules = xmlDoc.SelectNodes("/CodeMetricsReport/Targets/Target/Modules/Module");
  foreach (XmlNode module in modules)
  {
    foreach (XmlNode nodes in module)
    {
      switch (nodes.Name)
      {                        
        case "Metrics":
        foreach (XmlNode metric in nodes)
        {
          switch (metric.Attributes["Name"].Value)
          {
            case "MaintainabilityIndex":
            Console.WriteLine("MaintainabilityIndex={0}", metric.Attributes["Value"].Value);                                        
            break;
          }
        }
      }
    }
  }
  </code></pre>
  
  <p>This is the PowerShell thatt I've come up with thus far, but I'm having a little trouble. </p>
  
  <pre><code>[xml]$xmlDoc = New-Object System.Xml.XmlDocument
  $xmlDoc.Load($MetricsFileName)
  
  $modules = $xmlDoc.SelectNodes("/CodeMetricsReport/Targets/Target/Modules/Module")
  foreach ($module in $modules)
  {
    foreach ($nodes in $module)
    {
      Switch ($nodes.Name)
      {
        "Metrics"
        {
          foreach ($metric in $nodes)
          {
            Switch ($metric.Attributes["Name"].Value)
            {
              "MaintainabilityIndex"
              {
                Write-Host ("MaintainabilityIndex={0}" -f $metric.Attributes["Value"].Value) 
              }
            }       
          }
        }  
      }
    }
  }
  </code></pre>
  
  <p>When the script reaches the Switch statement, $nodes.Name evaluates to "Data.dll" in stead of the element name "Metrics", so the script never gets any further along. </p>
  
  <p>I've looked over this script for quite sometime and I can't figure out how to correct that. Any guidance is greatly appreciated! If there is a better way, I'd be happy to hear about that, too.</p>