循环遍历 XML 创建了正确数量的节点,但属性值是捆绑在一起的



我有这个Powershell函数:

function reportxml {
$logdir = "C:\DocumentsscriptOutput"
$XML_Path = $logdir + 'TR.xml'
$XML_NEW_PATH = $logdir + 'TestResult.xml'
$DLL_PATH = "tests.dll"
$xmlsettings = New-Object System.Xml.XmlWriterSettings
$xmlsettings.Indent = $true
$xmlsettings.IndentChars = "`t"
$XmlWriter = [System.XML.XmlWriter]::Create("$logdirTestResult.xml", $xmlsettings)
$xmlWriter.WriteStartDocument()
$xmlWriter.WriteStartElement('assemblies')
$xmlWriter.WriteEndElement()
$xmlWriter.WriteEndDocument()
$xmlWriter.Flush()
$xmlWriter.Close()
$xmlOld = [System.Xml.XmlDocument](Get-Content $XML_Path);
$xmlDoc = [System.Xml.XmlDocument](Get-Content "$logdirTestResult.xml");
$siteCollectionNode = $xmlDoc.CreateElement("assembly")
$xmlDoc.SelectSingleNode("//assemblies").AppendChild($siteCollectionNode)
$siteCollectionNode.SetAttribute("name", $DLL_PATH)
$siteCollectionNode.SetAttribute("total", $xmlOld.TestRun.ResultSummary.Counters.total)
$siteCollectionNode.SetAttribute("passed", $xmlOld.TestRun.ResultSummary.Counters.passed)
$siteCollectionNode.SetAttribute("failed", $xmlOld.TestRun.ResultSummary.Counters.failed)
$xmlDoc.Save($XML_NEW_PATH)
$siteCollectionNode.AppendChild($xmlDoc.CreateElement("errors"));
$xmlDoc.Save($XML_NEW_PATH)
$subSitesNode = $siteCollectionNode.AppendChild($xmlDoc.CreateElement("collection"))
$subSitesNode.SetAttribute("Name", "UAT Smoke Tests")
$subSitesNode.SetAttribute("total", $xmlOld.TestRun.ResultSummary.Counters.total)
$subSitesNode.SetAttribute("passed", $xmlOld.TestRun.ResultSummary.Counters.passed)
$subSitesNode.SetAttribute("failed", $xmlOld.TestRun.ResultSummary.Counters.failed)
$xmlDoc.Save($XML_NEW_PATH)
$TestResultNodes = $xmlOld.TestRun.Results.UnitTestResult 
$TestResultNodes | ForEach-Object {
$subSiteNameNode =  $subSitesNode.AppendChild($xmlDoc.CreateElement("test"));
$subSiteNameNode.SetAttribute("method",$xmlOld.TestRun.Results.UnitTestResult.testName)
$subSiteNameNode.SetAttribute("result", $xmlOld.TestRun.Results.UnitTestResult.outcome)
$subSiteNameNode.AppendChild($xmlDoc.CreateElement("output"))
$xmlDoc.Save($XML_NEW_PATH)
$ListsElement =  $subSiteNameNode.AppendChild($xmlDoc.CreateElement("traits"));
$ListElement = $ListsElement.AppendChild($xmlDoc.CreateElement("trait"));
$ListElement.SetAttribute("name", "Category")
$ListElement.SetAttribute("value", $xmlOld.TestRun.Results.UnitTestResult.testName)
$xmlDoc.Save($XML_NEW_PATH)
}
return $XML_NEW_PATH

}

工作几乎可以正常工作,它只是将所有测试名称和输出值放在每个名称和值属性中,如下所示:

<collection Name="UAT Smoke Tests" total="12" passed="0" failed="12">
<test method="Should_Search_LeaseByBookingStatusCommenced_Successfully Should_Search_LegalEntity_Successfully Should_Search_SequesnceNumberLease_Successfully Should_Search_PayableInvoice_Successfully Should_Search_CustomerByStatus_Successfully Should_Search_LeaseByBookingStatusPending_Successfully Should_Search_VendorByName_Successfully Should_Search_CustomerByNumber_Successfully Should_Search_VendorByNumber_Successfully Should_Search_CustomerByName_Successfully Should_Search_InvoiceNumber_Successfully Should_Search_CustomerByAlias_Successfully" result="Failed Failed Failed Failed Failed Failed Failed Failed Failed Failed Failed Failed">
<output />
<traits>
<trait name="Category" value="Should_Search_LeaseByBookingStatusCommenced_Successfully Should_Search_LegalEntity_Successfully Should_Search_SequesnceNumberLease_Successfully Should_Search_PayableInvoice_Successfully Should_Search_CustomerByStatus_Successfully Should_Search_LeaseByBookingStatusPending_Successfully Should_Search_VendorByName_Successfully Should_Search_CustomerByNumber_Successfully Should_Search_VendorByNumber_Successfully Should_Search_CustomerByName_Successfully Should_Search_InvoiceNumber_Successfully Should_Search_CustomerByAlias_Successfully" />
</traits>
</test>
</collection>

如何在第一个测试标签方法和值属性(当然,对于特征标签类别属性(中获取第一组值,在第二个标签中获取第二组值,依此类推?

我在网上研究了这个,但找不到与此案例类似的任何内容,非常感谢您的帮助。

我想通了,为处于相同情况的人留下了解决方案。 我使用数组来保留每个属性值,并将ForEach-Object切换到一个简单的for循环。请参阅下面的代码部分:

For ($i = 1; $i -lt $xmlOld.TestRun.ResultSummary.Counters.total; $i++ ) 
{
$testName = $xmlOld.TestRun.Results.UnitTestResult.testName
$testOutcome = $xmlOld.TestRun.Results.UnitTestResult.outcome
$testOutput = $xmlOld.TestRun.Results.UnitTestResult.Output.StdOut
$arrayTestNames = @{}
$arrayOutcomes = @{}
$arrayOutput = @{}
foreach ($item in $testName) {
$hostname = $item.Node.InnerText.Trim()
$services = @($item.Node.ParentNode.Service)
foreach ($service in $services)
{
$arrayTestNames["$hostname"] += @($service.Trim())}
}
$arrayTestNames
foreach ($result in $testOutcome) {
$hostname = $result.Node.InnerText.Trim()
$services = @($result.Node.ParentNode.Service)
foreach ($service in $services)
{
$arrayOutcomes["$hostname"] += @($service.Trim())}
}
$arrayOutcomes
foreach ($output in $testName) {
$hostname = $output.Node.InnerText.Trim()
$services = @($output.Node.ParentNode.Service)
foreach ($service in $services)
{
$arrayOutput["$hostname"] += @($service.Trim())}
}
$arrayOutput
$subSiteNameNode =  $subSitesNode.AppendChild($xmlDoc.CreateElement("test"));
$subSiteNameNode.SetAttribute("method", $testName[$i])
$subSiteNameNode.SetAttribute("result", $testOutcome[$i])

$outputNode = $subSiteNameNode.AppendChild($xmlDoc.CreateElement("output"))
$outputNode.AppendChild($xmlDoc.CreateTextNode($testOutput[$i])); 
$xmlDoc.Save($XML_NEW_PATH)
$ListsElement =  $subSiteNameNode.AppendChild($xmlDoc.CreateElement("traits"));
$ListElement = $ListsElement.AppendChild($xmlDoc.CreateElement("trait"));
$ListElement.SetAttribute("name", "Category")
$ListElement.SetAttribute("value", $testName[$i])
$xmlDoc.Save($XML_NEW_PATH)
}

}

return $XML_NEW_PATH

相关内容

最新更新