My Technical Notes

Monday, 29 February 2016

PowerShell - interating through XML Nodes

With PowerShell, you can easily iterate through XML nodes using the `Select-Xml` commandlet:


Select-Xml -Path <path> -Namespace @{ ( <prefix> = "<namespace>" )* } -XPath <xpath>

If we select the `Node` property of each incoming object using `% Node`, then it can be treated as an ordinary object with properties for each attribute and element underneath it.

For example (scroll right):


Select-Xml -Path 'c:\path\to\xml.xml' -Namespace @{ ( 'x' = "http://tahirhassan.blogspot.com" )* } -XPath '//x:hello/*' | % Node | % { $_.hello + ' ' + $_.world }

No comments: