user310291
user310291

Reputation: 38228

How to import CHILDREN nodes from one XML file to another

This post is about importing a whole XML file into another. I do not want to import WHOLE, I want to import CHILDREN. I tried to do this, but it doesn't work:

$xml_catalog=[xml]@'
<catalogue date_maj="2015-10-10T19:04:51">
                <products>
                               <product id="1">Pdt1</product>
                               <product id="2">Pdt2</product>
                </products>
                <categories>
                               <category id="1">cat1</category>
                               <category id="2">cat2</category>
                </categories>  
</catalogue>
'@

$xml_catalog_target = [xml]@'
<?xml version="1.0" encoding="iso-8859-1"?>
<catalogue date_maj="2015-10-12T19:04:51">
</catalogue>
'@

Foreach ($Node in $xml_catalog.DocumentElement.ChildNodes) {
    $xml_catalog_target.DocumentElement.AppendChild($xml_catalog.ImportNode($Node, $true))
}

Upvotes: 0

Views: 1368

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200493

Read closely. You need to import the nodes into the target XML structure:

Foreach ($Node in $xml_catalog.DocumentElement.ChildNodes) {
    $xml_catalog_target.DocumentElement.AppendChild($xml_catalog_target.ImportNode($Node, $true))
}

Upvotes: 2

Related Questions