Question:
I have a directory of very large XML files with a structure as this:
file1.xml:
1 2 3 4 5 6 |
file2.xml:
1 2 3 4 5 6 |
Now I am looking for a simple way to merge these files (*.xml) files into one output file:
1 2 3 4 5 6 7 8 9 |
I was thinking about using pure XSLT such as this one:
1 2 3 4 5 6 7 8 9 10 |
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> |
This works but isn’t as flexible as I want. Being a novice with PowerShell (version 2) eager to learn new best pracctices of working with XML in PowerShell I am wondering what is the simplest, purest PowerShell way of merging the structre of XML documents into one?
Cheers,
Joakim
Answer:
While the XSLT way to do this is pretty short, so is the PowerShell way:
1 2 3 4 5 6 7 8 |
$finalXml = " foreach ($file in $files) { [xml]$xml = Get-Content $file $finalXml += $xml.InnerXml } $finalXml += "" ([xml]$finalXml).Save("$pwd\final.xml") |
Hope this helps,