C# 使用Powershell解析XML代码度量报告
我希望我能得到一些帮助来克服这个脚本编写困难。我正在尝试解析这个XML。目前它是通过C#完成的,但我正试图在powershell中重新编写它C# 使用Powershell解析XML代码度量报告,c#,xml,powershell,xml-parsing,powershell-3.0,C#,Xml,Powershell,Xml Parsing,Powershell 3.0,我希望我能得到一些帮助来克服这个脚本编写困难。我正在尝试解析这个XML。目前它是通过C#完成的,但我正试图在powershell中重新编写它 <CodeMetricsReport Version="10.0"> <Targets> <Target Name="C:\Builds\APP\APP_v1.0.0\Data.dll"> <Modules> <Module Name="Data.dll" Ass
<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>
到目前为止,这是我提出的一个PowerShell,但我遇到了一个小麻烦
[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)
}
}
}
}
}
}
}
当脚本到达Switch语句时,$nodes.Name的计算结果为“Data.dll”,而不是元素名“Metrics”,因此脚本永远不会继续执行下去
我已经看过这个脚本很长一段时间了,我不知道如何纠正它。非常感谢您的指导!如果有更好的方法,我也很高兴听到这个消息。仅供参考,您也可以在PowerShell中使用
XPath来实现这一点:
$xml = [xml] (Get-Content $MetricsFileName)
foreach ($metric in $xml.CodeMetricsReport.Targets.Target.Modules.Module.Metrics.Metric) {
switch ($metric.Name) {
'MaintainabilityIndex' {
Write-Host "MaintainabilityIndex=$($metric.Value)"
}
}
}
$xml = [xml](Get-Content foo.xml)
$selectInfo = Select-Xml -Xml $xml -XPath '/CodeMetricsReport/Targets/Target/Modules/Module/Metrics/Metric[@Name="MaintainabilityIndex"]'
"MaintainabilityIndex=$($selectInfo.Node.Value)"
谢谢,伙计们!我要两个都试一下,看看哪一个对我最合适。
$xml = [xml](Get-Content foo.xml)
$selectInfo = Select-Xml -Xml $xml -XPath '/CodeMetricsReport/Targets/Target/Modules/Module/Metrics/Metric[@Name="MaintainabilityIndex"]'
"MaintainabilityIndex=$($selectInfo.Node.Value)"