如何在PowerShell中打印xml元素的实际内容?

问题描述:

当试图使用PowerShell解析xml文件中的各种数据时,能够查看实际所需的各种元素以及调试等将很有用。所以,如果我有这样的事情:如何在PowerShell中打印xml元素的实际内容?

[xml]$xmldoc = Get-Content doc.xml 
$node = $xmldoc.SelectSingleNode("contents/some/path/items/item[name='itemname']") 
$items = $xmldoc.contents.some.path.items 

write-host "items = [${items}], node = [${node}]" 

我希望能够看到的$项目和$节点的内容,但是我得到的是System.Xml.XmlDocument或System.Xml.XmlElement。当我尝试使用items.Value或items.InnerText时,它们显示为空,那么如何打印这些元素的实际文本?

结帐the XmlElement class。具体来说就是属性。我怀疑你是在.InnerXml.OuterXml之后。

+0

我得到所有的空回报,甚至$ {node.IsEmpty}返回一个空...或者我打印错误吗?我完全不知道PowerShell。 – Maxim

+0

尝试使用子表达式:'Write-Output“$($ items.OuterXml)$($ node.OuterXml)”' – gms0ulman

+0

好的,这似乎工作,是否有任何指导使用$ {} vs $()等等? – Maxim