开发者

How to compare two XML files and add missing elements using c#

Occasionally, one or more XML Elements are missing from XML File. Right now, one work around i am thinking is to compare XML file with a master XML File (having all the elements) and if any element is missing then add that element in XML File from master file.

How i gonna achieve this or any o开发者_如何学运维ther better idea?


One of the coolest libraries I've found while just looking around the Microsoft site is the XmlDiffPatch library. You can find more information at http://msdn.microsoft.com/en-us/library/aa302294.aspx but it essentially allows you to compare two documents, find all the differences and then apply those differences. Very useful for compressing xml files to send across a network


From link text

using System.Xml;

public class Form1
{

XmlDocument Doc1;
XmlDocument Doc2;

string Doc1Path = "C:\\XmlDoc1.xml";

private void Form1_Load(object sender, System.EventArgs e)
{

    Doc1 = new XmlDocument();
    Doc2 = new XmlDocument();

    Doc1.Load(Doc1Path);
    Doc2.Load("C:\\XmlDoc2.xml");

    Compare();

    Doc1.Save(Doc1Path);

}

public void Compare()
{

    foreach (XmlNode ChNode in Doc2.ChildNodes) {
        CompareLower(ChNode);
    }

}

public void CompareLower(XmlNode NodeName)
{

    foreach (XmlNode ChlNode in NodeName.ChildNodes) {

        if (ChlNode.Name == "#text") {
            continue;
        }

        string Path = CreatePath(ChlNode);

        if (Doc1.SelectNodes(Path).Count == 0) {
            XmlNode TempNode = Doc1.ImportNode(ChlNode, true);
            Doc1.SelectSingleNode(Path.Substring(0, Path.LastIndexOf("/"))).AppendChild(TempNode);
            Doc1.Save(Doc1Path);
        }
        else {
            CompareLower(ChlNode);
            Doc1.Save(Doc1Path);
        }

    }

}

public string CreatePath(XmlNode Node)
{

    string Path = "/" + Node.Name;

    while (!(Node.ParentNode.Name == "#document")) {
        Path = "/" + Node.ParentNode.Name + Path;
        Node = Node.ParentNode;
    }
    Path = "/" + Path;
    return Path;

}

}


In my example, the top level node is <content> so you'd just need to replace that for whatever your top level node is in your XML file.

public static void MergeMissingContentFileNodes(string sourceFile, string destFile)
{
    string topLevelNode = "content";

    XmlDocument srcXml = new XmlDocument();
    srcXml.Load(sourceFile);

    XmlDocument destXml = new XmlDocument();
    destXml.Load(destFile);

    XmlNode srcContentNode = srcXml.SelectSingleNode(topLevelNode);
    destXml = LoopThroughAndCreateMissingNodes(destXml, srcContentNode, topLevelNode);
    destXml.Save(destFile);

}

public static XmlDocument LoopThroughAndCreateMissingNodes(XmlDocument destXml, XmlNode parentNode, string parentPath)
{
    foreach (XmlNode node in parentNode.ChildNodes) 
    {
        //check if node exists and update destXML
        if (node.NodeType == XmlNodeType.Element) 
        {
            string currentPath = string.Format("{0}/{1}", parentPath, node.Name);
            if (destXml.SelectSingleNode(currentPath) == null) 
            {
                dynamic destParentNode = destXml.SelectSingleNode(parentPath);
                destParentNode.AppendChild(destParentNode.OwnerDocument.ImportNode(node, true));
            }

            LoopThroughAndCreateMissingNodes(destXml, node, currentPath);

        }
    }

    return destXml;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜