I need to copy node with name \"ProjectOptions\" from default.xml to original.xml without modifying anything else:
Original.xml
You try to insert the imported node under the DocumentElement
node, but $Child
is not a direct child element of that node. You need to call the InsertAfter()
method on the parent node of $Child
.
Change this:
$xml.DocumentElement.InsertAfter($XML.ImportNode($xmld.SelectSingleNode("//KEY[@ObjectName = 'ProjectOptions']"), $true), $Child)
into this:
$Child.ParentNode.InsertAfter($XML.ImportNode($xmld.SelectSingleNode("//KEY[@ObjectName='ProjectOptions']"), $true), $Child)
and the problem will disappear.
As a side note, you may want to use an XPath expression instead of dot-notation for selecting $Child
:
$Child = $xml.SelectSingleNode('//VALUE[@ObjectName="ShowWelcomeMsg"]')
Found a simple way:
$xmlPO = $xml.SelectNodes("//KEY[@ObjectName='ProjectOptions']")
$xmldPO = $xmld.SelectNodes("//KEY[@ObjectName='ProjectOptions']")
$xmlPO.set_InnerXML($xmldPO.innerXML)
$xml.Save($dpath)