Author Posts

January 1, 2012 at 12:00 am

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:




....


....




I use an xpath query with Select-XML and grab some quantity of 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 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 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 = @'



....


....



'@

$regex = @'
(?ms)\s*\s*
\s*\s*
(.+?)
\s*
\s*
\s*
\s*
'@

$xmltext -replace $regex,'$1'

....


....

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!