Parsing non-consistent XML-object using Poweshell



I'm trying to parse an XML which doesn't consist of uniform attributes. Example



<root>
<object>
<name>Object1</name>
<valueString>string</valueString>
</object>
<object>
<name>Object2</name>
<valueBoolean>true</valueBoolean>
</object>
</root>


The number of sub attributes is always the same but sometimes its a boolean and sometimes its a string and the tage name changes accordingly. The issue is not the type itself but with Powershell I have to know the tag name beforehand to obtain the value attached to it.


At the moment I'm doing something like this:



foreach($item in $items){
if(!$item.object.valueString){
$temp = $item.object.valueBoolean
}
else{
$temp = $item.object.valueString
}
$properties = @{
Name = $item.object.Name
value = $temp
}
}


This works but I would like a more elegant solution + at the moment its quite rigid and you have to cater for each and every possibility.


Is there a better way ?


Cheers


No comments:

Post a Comment