I want to remove some parent nodes from a XML document but keep the children. For example:
I have this structure:
<?xml version="1.0"?>
<ns:root xmlns:ns="ns">
<ns:node1>
<ns:node2>
<ns:node3>
</ns:node3>
<ns:node4>
</ns:node4>
</ns:node2>
</ns:node1>
</ns:root>
and want to get this:
<?xml version="1.0"?>
<ns:node2 xmlns:ns="ns"> <!-- new root -->
<ns:node3>
</ns:node3>
<ns:node4>
</ns:node4>
</ns:node2>
My current script looks like this:
# Get the content as xml
$xml = [xml] (gc c:\test.xml)
# Grab the namespace
$namespaceMgr = New-Object System.Xml.XmlNamespaceManager $xml.NameTable
$namespace = $xml.DocumentElement.NamespaceURI
$namespaceMgr.AddNamespace("ns", $namespace)
# Select node2
$xml.SelectSingleNode('/ns:root/ns:node1/ns:node2', $namespaceMgr)
# How to save it now as a new valid xml file?