Parsing XML Code Metrics Report using Powershell



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.



<CodeMetricsReport Version="10.0">
<Targets>
<Target Name="C:\Builds\APP\APP_v1.0.0\Data.dll">
<Modules>
<Module Name="Data.dll" AssemblyVersion="1.0.0" FileVersion="1.0.0">
<Metrics>
<Metric Name="MaintainabilityIndex" Value="84" />
<Metric Name="CyclomaticComplexity" Value="39" />
<Metric Name="ClassCoupling" Value="14" />
<Metric Name="DepthOfInheritance" Value="1" />
<Metric Name="LinesOfCode" Value="101" />
</Metrics>
</Module>
</Modules>
</Target>
</Targets>
</CodeMetricsReport>


This is a snippet of the C# code that I'm currently using to parse the above XML.



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;
}
}
}
}
}


This is the PowerShell thatt I've come up with thus far, but I'm having a little trouble.



[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)
}
}
}
}
}
}
}


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.


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.


Tiada ulasan:

Catat Ulasan