in powershell, given following partial xml structure:
<extraattributes enabled="1" perbuildingenabled="0" perbuildingmode="0"> <!--perbuildingmode 0: inclusively district , building, mode 1: exclusively building--> <attributes> <a enabled="1"> <name>mail</name> <value>$userprincipalname</value> </a> <a enabled="1"> <name>ipphone</name> <value>123456</value> </a> </attributes> </extraattributes> if try access value of 1 of value elements following:
# $extraattributes extracted [xml.xmlelement] [xml.xmldocument] foreach ($att in ($extraattributes.attributes.childnodes | { [int]$_.enabled -eq $true })) { write-host "name: $($att.name), value: $($att.value)" } it works fine...however, if there single a element under attributes so:
<extraattributes enabled="1" perbuildingenabled="0" perbuildingmode="0"> <!--perbuildingmode 0: inclusively district , building, mode 1: exclusively building--> <attributes> <a enabled="1"> <name>mail</name> <value>$userprincipalname</value> </a> </attributes> </extraattributes> powershell thinks $att.value $null.
how can access element values when single a element present same foreach loop? xml can restructured, i'm trying avoid that.
instead of using childnodes, used element name a such:
foreach ($att in ($extraattributes.attributes.a | { [int]$_.enabled -eq $true })) { write-host "name: $($att.name), value: $($att.value)" } and works expected whether there 1 or multiple a elements. never figured out why childnodes doesn't allow work.
Comments
Post a Comment