PowerShell XML Nodes from Another File
<p>I'm running my PowerShell script to basically create a big XML file from several smaller XML files.</p>

<p>The script opens Template.txt:</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;Model&gt;
  &lt;LobSystems&gt;
    &lt;LobSystem&gt;
      &lt;LobSystemInstances&gt;
      &lt;/LobSystemInstances&gt;  
      &lt;Entities&gt;
        &lt;!-- individual &lt;ENTITY&gt; from every XML file goes here --&gt;
      &lt;/Entities&gt;      
    &lt;/LobSystem&gt;
  &lt;/LobSystems&gt;
&lt;/Model&gt;
</code></pre>

<p>All I want to do is to copy the node <code>&lt;Entity&gt;</code> from each XML file in a given folder and create a new MASTER.XML using the template and information and extracting <code>&lt;ENTITY&gt;</code> from every .xml in the folder, resulting this:</p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;Model&gt;
  &lt;LobSystems&gt;
    &lt;LobSystem&gt;
      &lt;LobSystemInstances&gt;
      &lt;/LobSystemInstances&gt;  
      &lt;Entities&gt;

          &lt;Entity Name="A"&gt;       // from File1.XML
             &lt;Value&gt;XYZ&lt;/Value&gt;
          &lt;/Entity&gt;

          &lt;Entity Name="B"&gt;       // from File4.XML
             &lt;Value&gt;123&lt;/Value&gt;
          &lt;/Entity&gt;

          &lt;Entity Name="C"&gt;       // from File3.XML
             &lt;Value&gt;@#$&lt;/Value&gt;
          &lt;/Entity&gt;

      &lt;/Entities&gt;      
    &lt;/LobSystem&gt;
  &lt;/LobSystems&gt;
&lt;/Model&gt;
</code></pre>

<p>So far my script is the following:</p>

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

<p>Well it is not working.... I keep getting error messages about AppendChild()</p>

<p>Any suggestions???</p>