2017-08-03 25 views
4

當試圖使用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時,它們顯示爲空,那麼如何打印這些元素的實際文本?

回答

4

結帳the XmlElement class。具體來說就是屬性。我懷疑你是在.InnerXml.OuterXml之後。

+0

我得到所有的空回報,甚至$ {node.IsEmpty}返回一個空...或者我打印錯誤嗎?我完全不知道PowerShell。 – Maxim

+0

嘗試使用子表達式:'Write-Output「$($ items.OuterXml)$($ node.OuterXml)」' – gms0ulman

+0

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