【发布时间】:2018-08-28 22:10:44
【问题描述】:
我正在尝试使用 PowerShell 遍历 XML,但无法获取 InnerXML 详细信息。
XML:
<script>
<data>
<address>
<street>WhiteField</street>
<zip>560100</zip>
<city>Bangalore</city>
<country>India</country>
<postofficebox>BangaloreEast</postofficebox>
</address>
</data>
<data>
<address>
<street>Gurgaon</street>
<zip>601000</zip>
<city>New Delhi</city>
<country>India</country>
<postofficebox>New Delhi West</postofficebox>
</address>
</data>
</script>
脚本:
[string]$FilePath = 'C:\Users\Sujeet\Desktop\UserData.xml'
try {
[xml]$XMLInput = Get-Content -Path $FilePath
} catch {
Write-Host "Failed to read or parse the XML File."
exit
}
if ((Select-Xml -Xml $XMLInput -XPath "//address" | measure).Count -gt 0) {
Select-Xml -Xml $XMLInput -XPath "//address" | foreach {
$_.Node.InnerXML
}
}
如何获取<street>、<city>、<country> 的值?
此外,我只想在城市为“班加罗尔”时获取详细信息。
【问题讨论】:
标签: xml powershell