1

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.

2 Answers 2

3

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"]')
Sign up to request clarification or add additional context in comments.

Comments

2

Found a simple way:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.