>我正在尝试循环访问XML文件并显示节点名称。 最终目标是让用户选择一个选项,然后引用该节点的值。 下面的代码仅输出以下内容:
> builds
我希望它输出这个:
> LastKnownGood
> Latest
我做错了什么?
RunBuild.ps1
$buildsFile = [System.Xml.XmlDocument](Get-Content "$($config.buildspath)/builds.xml");
$builds = $buildsFile.builds;
Write-Host "Available Builds: "
ForEach ($buildVersion in $builds) {
Write-Host " > $($buildVersion.Name)"
}
构建.xml
<builds>
<LastKnownGood>Build 8</LastKnownGood>
<Latest>Build 18</Latest>
</builds>
将ForEach ($buildVersion in $builds)
更改为ForEach ($buildVersion in $builds.ChildNodes)
,它现在按预期工作。
ForEach ($buildVersion in $builds.ChildNodes) {
Write-Host " > $($buildVersion.LocalName)"
}