Powershell XML importnode from another file

The contents of profile.xml:

<files>
  <file folder="CaptureServer" filename="CSConfig" object="CSConfig">
    <Profile name="BBH1200Kofax">
      <OutputCache>\</OutputCache>
      <EncryptedConnectionString>564rgr=</EncryptedConnectionString>
      <ConvertDocsBeforeRelease>false</ConvertDocsBeforeRelease>
    </Profile>
  </file>
  <file folder="CaptureServices3" filename="CSConfig" object="CSConfig">
    <Profile name="BBH1200Kofax">
      <ReleaseToEnterprise>true</ReleaseToEnterprise>
      <CaptureServerUrl />
      <OutputCache />
      <Credentials>
        <EncryptedPassword>46s4rg=</EncryptedPassword>
        <UserName />
        <Domain />
      </Credentials>
      <ConvertDocsBeforeRelease>false</ConvertDocsBeforeRelease>
    </Profile>
  </file>
</files>

The contents of the rules. xml:

<file folder="" filename="Rules" object="ArrayOfIBarcodeRule">
  <Profile name="Test471">
    <IBarcodeRule>
      <RuleName>DOC-TESTTESTTEST-Code128</RuleName>
      <FieldSequenceNumber>1</FieldSequenceNumber>
      <FieldRectangle>
        <Location>
          <X>0</X>
          <Y>0</Y>
        </Location>
        <Size>
          <Width>0</Width>
          <Height>0</Height>
        </Size>
      </FieldRectangle>
      <SeparationValue>TESTTESTTEST</SeparationValue>
    </IBarcodeRule>
  </Profile>
</file>

I am trying to add the entire contents of rules.xml (node ​​file) as another node in profile.xml. As you can see, there are many other file nodes in the profile.xml file, and rules.xml is another one.

This is the code I tried and it does nothing:

$xml = [xml](Get-Content ".\profile.xml")
$newxml = [xml](Get-Content ".\rules.xml")
$xml.ImportNode($newxml.get_DocumentElement(), $true)
$xml.Save(".\profile.xml")
+5
source share
1 answer

You are really close, but ImportNode only makes a copy and does not actually paste the copied nodes into the document. Try the following:

$newNode = $newxml.ImportNode($xml.get_DocumentElement(), $true)
$newxml.DocumentElement.AppendChild($newNode)
$xml.Save("$pwd\profile.xml")  
+13
source

All Articles