Reorganizing node structure in xml

by Ritmo2k at 2013-02-15 09:23:49

I have an xml file I load in as a System.Xml.XmlDocument, the structure is roughly:

<level1>
<level2>
<object type=“a”>

</object type=“a”>
<object type=“b”>

</object type=“b”>
</level2>
</level1>

I use an xpath query with Select-XML and grab some quantity of <object> nodes based on the type attribute.
When parsing the object after Select-XML carves it up, I use these steps:

$xml_nodes = Select-Xml -Xml $XML -XPath $Query
Foreach ($xml_node in $xml_nodes)
{

$var = $xml_node.get_Node()
$var.something …

}

This provides me a means to iterate through the included <object> sets.
For the sake of continuity throughout the local end of the parsing and remote end when I convert it back to a string object to avoid serialization, the included <object> nodes regain the full structure. So accessing them becomes $var.level1.level2.object.something etc.

I need to avoid this as I share code on both ends, is it possible to remove the level1 and level2 branches on the far end so I can access the xml object the same way? You can remove nodes, but I dont see how at first glance to remove parent enclosing nodes leaving the child.

Thanks!
by mjolinor at 2013-02-15 12:10:19
If you don’t mind regex, I’ve got this:

$xmltext = @’
<level1>
<level2>
<object type=“a”>

</object type=“a”>
<object type=“b”>

</object type=“b”>
</level2>
</level1>
’@

$regex = @’
(?ms)\s*<level1>\s*
\s*<level2>\s*
(.+?)
\s*</level2>\s*
\s*</level1>\s*
’@


$xmltext -replace $regex,’$1’

<object type=“a”>

</object type=“a”>
<object type=“b”>

</object type=“b”>

It looks a little odd, but works well. I call it “jumbo shrimp in a can” - (?ms) = jumbo shrimp, the here-string is the “can”.
by Ritmo2k at 2013-02-16 15:31:36
Cool, I’ll give it a whirl, but it looks like it could do what I need.

Much appreciated!