I am struggling to find a reliable way to determine the names of elements that were selected by an XPath expression using the PowerShell cmdlet select-xml:
$xml = select-xml -content @'
<root>
<A name='foo' localname='FOO'/>
<B name='bar' />
<C id ='baz' localname='BAZ'/>
<D />
</root>
'@ -XPath '/root/*'
[System.Xml.XmlElement] $elem = $null
foreach ($elem in $xml.Node) {
"name = $($elem.name), localname = $($elem.localname)"
}
This code prints
name = foo, localname = FOO
name = bar, localname = B
name = C, localname = BAZ
name = D, localname = D
Apparently, the XML attributes name and localname interfere with the .NET class attributes with the same names. So, is there a construct that returns A, B, C and D for the above example.