Ilya Shapovalov
Ilya Shapovalov

Reputation: 31

Import node from one XML to another on PowerShell

I need to copy node with name "ProjectOptions" from default.xml to original.xml without modifying anything else:

Original.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<KEYS>
  <KEY ObjectName="computername_user" RegObjectType="0">
    <KEYS>
      <KEY ObjectName="Desktop" RegObjectType="0">
        <KEYS>
          <KEY ObjectName="Settings" RegObjectType="0">
            <KEYS>
              <KEY ObjectName="PrinterDefault" RegObjectType="0">
                <VALUES>
                  <VALUE ObjectName="PrinterOrientation" Value="2" ValueType="4" />
                </VALUES>
              </KEY>
              <KEY ObjectName="ProjectOptions" RegObjectType="0">
                <VALUES>
                  <VALUE ObjectName="ShowWelcomeMsg" Value="0" ValueType="4" />
                </VALUES>
              </KEY>
            </KEYS>
          </KEY>
        </KEYS>
      </KEY>
    </KEYS>
  </KEY>
</KEYS>

Default.xml

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<KEYS>
  <KEY ObjectName="computername_user" RegObjectType="0">
    <KEYS>
      <KEY ObjectName="Desktop" RegObjectType="0">
        <KEYS>
          <KEY ObjectName="Settings" RegObjectType="0">
            <KEYS>
              <KEY ObjectName="PrinterDefault" RegObjectType="0">
                <VALUES>
                  <VALUE ObjectName="PrinterOrientation" Value="2" ValueType="4"/>
                </VALUES>
              </KEY>
              <KEY ObjectName="ProjectOptions" RegObjectType="0">
                <VALUES>
                  <VALUE ObjectName="GSAddBatchOptionDialogRect" Value="381,203,981,629" ValueType="2"/>
                  <VALUE ObjectName="GSHeadNodeName" Value="" ValueType="2"/>
                  <VALUE ObjectName="GSIsAdvancedMode" Value="1" ValueType="4"/>
                  <VALUE ObjectName="GSRemoteSchedulerPlatform" Value="" ValueType="2"/>
                  <VALUE ObjectName="GSSchedulerName" Value="" ValueType="2"/>
                  <VALUE ObjectName="GSShowFrequentlyUsedBatchOptions" Value="1" ValueType="4"/>
                  <VALUE ObjectName="GSUserName" Value="" ValueType="2"/>
                  <VALUE ObjectName="ShowWelcomeMsg" Value="0" ValueType="4"/>
                </VALUES>
              </KEY>
            </KEYS>
          </KEY>
        </KEYS>
      </KEY>
    </KEYS>
  </KEY>
</KEYS>

I tried something like this

$xml = [xml](Get-Content "C:\Temp\original.xml")
$xmld = [xml](Get-Content "C:\Temp\default.xml")
$Child=$xml.KEYS.KEY.KEYS.KEY.KEYS.KEY.KEYS.KEY[1].VALUES.VALUE
$xml.DocumentElement.InsertAfter($XML.ImportNode($xmld.SelectSingleNode("//KEY[@ObjectName = 'ProjectOptions']"), $true), $Child)
$xml.Save("C:\Temp\save.xml")

But it ended with "The reference node is not a child of this node." Please tell me where I went wrong. Thanks.

Upvotes: 1

Views: 1247

Answers (2)

Ilya Shapovalov
Ilya Shapovalov

Reputation: 31

Found a simple way:

$xmlPO = $xml.SelectNodes("//KEY[@ObjectName='ProjectOptions']")
$xmldPO = $xmld.SelectNodes("//KEY[@ObjectName='ProjectOptions']")
$xmlPO.set_InnerXML($xmldPO.innerXML)
$xml.Save($dpath)​

Upvotes: 2

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200493

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"]')

Upvotes: 3

Related Questions