【发布时间】:2018-06-18 22:24:52
【问题描述】:
我已经制作了这个示例 XML 来循环遍历,我有兴趣为每个项目输出产品名称。
<example>
<item>
<productname>Hoover</productname>
</item>
<item>
<productname>TV</productname>
</item>
<item>
<productname>Microwave</productname>
</item>
<item>
<productname>Computer</productname>
</item>
</example>
在我的 powershell 中,我可以轻松地遍历每个项目,但我无法选择“产品名称”的内容,我不确定为什么。这是我的示例 powershell 代码:
[xml] $xml = Get-Content "./xmlexample.xml"
foreach ($item in $xml.example) {
Write-Host($item.InnerXML) #Outputs all XML as expected
Write-Host($item.InnerText) #Outputs the text of all child elements as expected
Write-Host($item.productname.InnerText) #Does not output anything
Write-Host($item.productname.InnerXML) #Does not output anything
}
任何关于为什么这不起作用的帮助或建议将不胜感激。它的行为就像一个没有其他子元素的子元素不会被以同样的方式对待。
【问题讨论】:
-
请尝试
foreach ($item in $xml.example.ChildNodes)
标签: xml powershell