0

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))
}
Community
  • 1
  • 1
user310291
  • 36,946
  • 82
  • 271
  • 487

1 Answers1

2

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))
}
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328