Q:

PowerShell XML Nodes from Another File

I'm running my PowerShell script to basically create a big XML file from several smaller XML files.
The script opens Template.txt:
<?xml version="1.0" encoding="utf-8"?>
<Model>
  <LobSystems>
    <LobSystem>
      <LobSystemInstances>
      </LobSystemInstances>  
      <Entities>
        <!-- individual <ENTITY> from every XML file goes here -->
      </Entities>      
    </LobSystem>
  </LobSystems>
</Model>

All I want to do is to copy the node <Entity> from each XML file in a given folder and create a new MASTER.XML using the template and information and extracting <ENTITY> from every .xml in the folder, resulting this:
<?xml version="1.0" encoding="utf-8"?>
<Model>
  <LobSystems>
    <LobSystem>
      <LobSystemInstances>
      </LobSystemInstances>  
      <Entities>

          <Entity Name="A">       // from File1.XML
             <Value>XYZ</Value>
          </Entity>

          <Entity Name="B">       // from File4.XML
             <Value>123</Value>
          </Entity>

          <Entity Name="C">       // from File3.XML
             <Value>@#$</Value>
          </Entity>

      </Entities>      
    </LobSystem>
  </LobSystems>
</Model>

So far my script is the following:
[xml]$master = get-content .\Template.txt
$files = get-item -Path .\*.xml -Exclude 'Master.xml'

foreach ($file in $files)
{
    [xml]$filecontents = get-content $file

    $entity = $fileContents.Model.LobSystems.LobSystem.Entities.Entity  

    $master.Model.LobSystems.LobSystem.Entities.Entity.AppendChild($entity);

}
$master.Save("Master.xml")

Well it is not working.... I keep getting error messages about AppendChild()
Any suggestions???

A:

Seems that you need to import the element first since it comes from another XML document instance, something like this :
foreach ($file in $files)
{
    [xml]$filecontents = get-content $file

    $entity = $fileContents.Model.LobSystems.LobSystem.Entities.Entity  

    $importedEntity = $master.ImportNode($entity, $TRUE)

    $master.Model.LobSystems.LobSystem.Entities.AppendChild($importedEntity);

}

