使用PowerShell读取XML文件并显示价值

时间:2019-05-13 08:04:56

标签: xml powershell file xml-parsing

我想在PowerShell中读取XML文件,并在success="True"中显示节点<First-suite>的值,但是无法正常工作。下面是我的XML文件。

<?xml version=1.0 encoding=""....?>
<First-results name="myassemble.dll" total="5" errors="0" failures="0">
  <database multiversion"2323" os-version="Win10" platform="32bit"/>
  <mssql-info current-current="en-A" current-aiculture="en-A"/>
  <First-suite type "myassembly" name="mydll" executed="true" result="Success" success="True" time="3.66">
    <MyTest>  </MyTest>
  </First-suite>
</First-results>

我的示例代码:

[xml]$xml = Get-Content 'c:\MyFile.xml'
$xml_FirstResults = $xml.SelectNodes('First-results /First-suite')
foreach ($Test in $xml_FirstResults) {
    Write-Host($Test.success)
}

1 个答案:

答案 0 :(得分:0)

这可以通过单个XPath查询来完成。

$xml | Select-Xml -XPath "//First-suite[@success='True']/*"