// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Xml; class MainClass { private static void process(XmlNode oldnode, XmlNode newnode) { XmlNodeList childs = oldnode.ChildNodes; foreach(XmlNode child in childs) { switch(child.NodeType) { case XmlNodeType.Element: XmlElement elm = newnode.OwnerDocument.CreateElement(child.Name); foreach(XmlAttribute an in child.Attributes) { elm.SetAttribute(an.Name, an.Value); } newnode.AppendChild(elm); process(child, elm); break; case XmlNodeType.Attribute: XmlAttribute attr = newnode.OwnerDocument.CreateAttribute(child.Name, child.Value); newnode.AppendChild(attr); break; case XmlNodeType.Text: XmlText txt = newnode.OwnerDocument.CreateTextNode(child.Value); newnode.AppendChild(txt); break; } } } public static void Main(string[] args) { string[] files = { @"C:\fil1.xml", @"C:\fil2.xml" }; XmlDocument newdoc = new XmlDocument(); XmlElement root = newdoc.CreateElement("root"); newdoc.AppendChild(root); for(int i = 0; i < files.Length; i++) { XmlDocument olddoc = new XmlDocument(); olddoc.Load(files[i]); process(olddoc.DocumentElement, newdoc.DocumentElement); } newdoc.Save(@"C:\allfiles.xml"); newdoc.Save(Console.Out); } }