Import node from one XML to another on PowerShell
<p>I need to copy node with name "ProjectOptions" from default.xml to original.xml without modifying anything else:</p>

<p>Original.xml </p>

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

<p>Default.xml</p>

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

<p>I tried something like this</p>

<pre><code>$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")
</code></pre>

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