Feature Post

Top

How to: Merge Multiple Xmls?

Easiest, simplest, fastest way...? if you dont want any checks to be performed(duplicates, zombies, etc), and are using .NET 3.5 or above

var ResultXml = XDocument.Load("file1.xml");
ResultXml.Root.Add(XDocument.Load("file2.xml").Root.Elements());

Or if you are "still" a fan of pre-daters, use XmlDocument:


static void AppendChildren(XmlWriter xmlWriter, string strFilePath)
{
    using (XmlReader xmlReader = XmlReader.Create(strFilePath))
    {
        xmlReader.MoveToContent();
        int nDepth = xmlReader.Depth + 1;
        if (xmlReader.Read())
        {
            while (xmlReader.Depth == nDepth)
            {
                xmlWriter.WriteNode(xmlReader, true);
            }
        }
    }
}
Or better, would be to make it an extension method:


static void AppendChildren(this XmlWriter xmlWriter, string strFilePath)

Happy programming! (0:

No comments