2

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???

2
  • 2
    I suggest: define not working..., include error messages in your post. Commented Mar 3, 2016 at 5:33
  • You cannot call a method on a null-valued expression. At Parser.ps1:17 char:5 + $master.Model.LobSystems.LobSystem.Entities.Entity.AppendChild($importedEnti ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull Commented Mar 5, 2016 at 0:08

1 Answer 1

2

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);

}
Sign up to request clarification or add additional context in comments.

2 Comments

Wondeful!!!! Got me the right thing working, but I had to remove one little mistake, instead of Entities.Entity. simply removed .Entity and everything worked fine!
Sure, AppendChild() should've been invoked on the parent of Entity. Thanks for the confirmation and correction, answer has been updated accordingly.

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.