If the intent is to get the value of attribute biscuit
without knowing its name from the <snacks>
element:
[xml] $xml = @'
<item>
<food>
<snacks biscuit="mariegold">
</snacks>
</food>
</item>
'@
$xml.item.food.snacks.Attributes.Value # -> 'mariegold'
The above relies on member-access enumeration to simply extract the values of all attributes, via the Attributes
collection; since there is only one attribute in this case (biscuit
), its value is returned.
If you're unexpectedly still running Windows PowerShell version 2, where member-access enumeration is not available, use the following instead:
$xml.item.food.snacks.Attributes | foreach { $_.Value } # -> 'mariegold'