1

I'm running a script in PowerShell to try and create one big XML file, it's actually very similar to this question: PowerShell XML Nodes from Another File

To that extent I'll borrow the code from the answer.

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

}

The main difference for me is that the variable entity returns a NodeList so I cannot use ImportNode.

Here is an example piece of XML that illustrates the point:

<?xml version="1.0" encoding="utf-8"?>
<!-- Created with Liquid Studio -->
<bookstore xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:noNamespaceSchemaLocation="BookStore.xsd">
    <book price="730.54" ISBN="string" publicationdate="2016-02-27">
        <title>string</title>
        <author>
            <first-name>string</first-name>
            <last-name>string</last-name>
        </author>
        <genre>string</genre>
    </book>
    <book price="6738.774" ISBN="string">
        <title>string</title>
        <author>
            <first-name>string</first-name>
            <last-name>string</last-name>
        </author>
    </book>
</bookstore>

Where in each file there are multiple Book items, so if I did the following:

$entity = $fileContents.BookStore.Book 

I'd get a NodeList.

Is there a way to loop through a NodeList and append each item as a node?

I also tried the following:

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

    $entity = $fileContents.rss.channel.item

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

    $master.rss.channel.AppendChild($importedEntity);
}
$master.Save("New-Master.xml")

Where the entity I want is a little deeper but the same principle applies.

1
  • 1
    Nested loops are a thing: foreach($entity in $fileContents.Model.LobSystems.LobSystem.Entities.Entity){ $master.ImportNode($entity,$true); <# ... #> } Commented Feb 22, 2022 at 13:41

0

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.