# Copyright 2008 Josh Erickson (josh dot sonshine at gmail dot com) # # mergeXML :: Merge two XML Documents into one. # # Everything from $from, will be copied to $to. Any duplicate # attributes will be overwritten with data from $from. Nodes in # $from will be appended to their mirror location in $to. # # Arguments # $from - The XML document that will be copied. # $to - XML document that $from will be copied to. # # function mergeXML { $from = $args[0][0]; $to = $args[0][1]; if(![bool]"$($from | gm)".contains("System.Xml.XmlDocument") -or ![bool]"$($to | gm)".contains("System.Xml.XmlDocument")) { return -1; } if(![bool]$from.get_HasChildNodes() -or ![bool]$to.get_HasChildNodes()) { return "-2:$($from.get_name()):$($to.get_name())"; } $fchild = $from.get_childnodes(); $tchild = $to.get_childnodes(); #foreach($fci in $fchild) { for([int]$i=0;$i -lt $fchild.count;$i++) { $fci = $fchild.item($i); if(($tci = $to."$($fci.get_name())") -eq $Null) { $e = $fci.cloneNode($True); if("$($fci.get_parentNode().get_name())" -eq "#document") { $to.ImportNode($fci,$True); $to.normalize(); } else { $tci = $to.get_ownerDocument().importNode($fci,$True) # | set-Variable -name tci; $to.AppendChild($tci); $to.normalize(); } } foreach($fcai in $fci.get_attributes()) { $tci.setAttribute($fcai.get_name(),$fcai.get_value()); } if($fci.get_HasChildNodes()) { mergeXML($fci,$tci); } } }