Click here to Skip to main content
15,885,435 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I try to find xml difference using Microsoft.XmlDiffPatch. But i did not get complete xml data as output. It removes last portion of xml data.

Here is the code
C#
string FindXMLDiff(string oldXmlString, string newXmlString, out string xmlDiff)
    {
    string htmlOut = string.Empty; xmlDiff = "";

        XmlDiff xmldif = new XmlDiff(XmlDiffOptions.IgnoreChildOrder |
                                         XmlDiffOptions.IgnoreNamespaces |
                                         XmlDiffOptions.IgnorePrefixes |
                                         XmlDiffOptions.IgnoreDtd |
                                         XmlDiffOptions.IgnoreWhitespace);

        System.IO.StringReader oldString = new System.IO.StringReader(oldXmlString);
        System.IO.StringReader newString = new System.IO.StringReader(newXmlString);
        System.Xml.XmlReader oldEReader = System.Xml.XmlReader.Create(oldString);
        System.Xml.XmlReader newEReader = System.Xml.XmlReader.Create(newString);
        StringBuilder diffElement = new StringBuilder();
        System.Xml.XmlWriter diffWriter = System.Xml.XmlWriter.Create(diffElement);

        bool bIdentical = xmldif.Compare(oldEReader, newEReader, diffWriter);
        diffWriter.Close();
        xmlDiff = diffElement.ToString();

        System.Xml.XmlTextReader difReader = new System.Xml.XmlTextReader(new System.IO.StringReader(diffElement.ToString()));
        System.Xml.XmlTextReader difReader1 = new System.Xml.XmlTextReader(new System.IO.StringReader(diffElement.ToString()));
        System.Xml.XmlTextReader orgReader = new System.Xml.XmlTextReader(new System.IO.StringReader(oldXmlString));

        System.Xml.XmlDocument diffgramDoc = new System.Xml.XmlDocument();
        diffgramDoc.Load(difReader1);
        XmlDiffView view = new XmlDiffView();
        if (diffgramDoc.DocumentElement.ChildNodes.Count > 0)
        {
            view.Load(orgReader, difReader);

            System.IO.MemoryStream stream = new System.IO.MemoryStream();
            System.IO.StreamWriter sw1 = new System.IO.StreamWriter(stream);
            sw1.Write("<html><body><table style='font-family:verdana; font-size:12'>");
            sw1.Write("<tr><td>Legend: <font style='background-color: yellow'" +
            " color='black'>added</font>  <font style='background-color: red'" +
            "color='black'>removed</font>  <font style='background-color: " +
            "lightgreen' color='black'>changed</font>" +
            "</td><td><font style='background-color: red' color='blue'>moved from</font>" +
            "  <font style='background-color: yellow' color='blue'>moved to" +
            "</font>  <font style='background-color: white' color='#AAAAAA'>" + "ignored</font></td></tr>");
            sw1.Write("<tr><td>Old");
            sw1.Write("</td><td>New");
            sw1.Write("</td></tr>");

            view.GetHtml(sw1);

            sw1.Write("</table></body></html>");

            var sr = new System.IO.StreamReader(stream);
            stream.Seek(0, System.IO.SeekOrigin.Begin);
            htmlOut = sr.ReadToEnd();

            sw1.Close();
        }
        else
        {
            htmlOut = "";
        }
        view = null;
        oldString.Close();
        newString.Close();
        oldEReader.Close();
        newEReader.Close();
        diffWriter.Close();
        difReader.Close();
        orgReader.Close();
        return htmlOut;

    }



In this values of "htmlOut" has some xml missing.


C#
oldXmlString = "<?xml version=\"1.0\" encoding=\"utf-8\"?><MyXml xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" version=\"1.0.0.1\"><TableData><Parameters><Parameter name=\"Width\"><MinValue>10</MinValue><MaxValue>100</MaxValue></Parameter><Parameter name=\"Height\"><MinValue>40</MinValue><MaxValue>600</MaxValue></Parameter><Parameter name=\"Depth\"><MinValue>34</MinValue><MaxValue>250</MaxValue></Parameter></Parameters></TableData></MyXml>";



C#
newXmlString = "<?xml version=\"1.0\" encoding=\"utf-8\"?><MyXml xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" version=\"1.0.0.1\"><TableData><Parameters><Parameter name=\"Width\"><MinValue>10</MinValue><MaxValue>100</MaxValue></Parameter><Parameter name=\"Height\"><MinValue>80</MinValue><MaxValue>600</MaxValue></Parameter><Parameter name=\"Depth\"><MinValue>34</MinValue><MaxValue>86</MaxValue></Parameter><Parameter name=\"Speed\"><MinValue>0</MinValue><MaxValue>10</MaxValue></Parameter></Parameters></TableData></MyXml>";



out put as

C#
<html><body><table style='font-family:verdana; font-size:12'><tr><td>Legend: <font style='background-color: yellow' color='black'>added</font>  <font style='background-color: red'color='black'>removed</font>  <font style='background-color: lightgreen' color='black'>changed</font></td><td><font style='background-color: red' color='blue'>moved from</font>  <font style='background-color: yellow' color='blue'>moved to</font>  <font style='background-color: white' color='#AAAAAA'>ignored</font></td></tr><tr><td>Old</td><td>New</td></tr><tr><td style="padding-removed 10pt;"><font style="background-color: white" color="black"><?xml version="1.0" encoding="utf-8"?></font></td><td style="padding-removed 10pt;"><font style="background-color: white" color="black"><?xml version="1.0" encoding="utf-8"?></font></td></tr><tr><td style="padding-removed 10pt;"><font style="background-color: white" color="black"><MyXml</font> <font style="background-color: white" color="#AAAAAA">xmlns:xsi</font><font style="background-color: white" color="#AAAAAA">="http://www.w3.org/2001/XMLSchema-instance"</font><br />       <font style="background-color: white" color="#AAAAAA">xmlns:xsd</font><font style="background-color: white" color="#AAAAAA">="http://www.w3.org/2001/XMLSchema"</font><br />       <font style="background-color: white" color="black">version="1.0.0.1"</font><font style="background-color: white" color="black">></font></td><td style="padding-removed 10pt;"><font style="background-color: white" color="black"><MyXml</font> <font style="background-color: white" color="#AAAAAA">xmlns:xsi</font><font style="background-color: white" color="#AAAAAA">="http://www.w3.org/2001/XMLSchema-instance"</font><br />       <font style="background-color: white" color="#AAAAAA">xmlns:xsd</font><font style="background-color: white" color="#AAAAAA">="http://www.w3.org/2001/XMLSchema"</font><br />       <font style="background-color: white" color="black">version="1.0.0.1"</font><font style="background-color: white" color="black">></font></td></tr><tr><td style="padding-removed 25pt;"><font style="background-color: white" color="black"><TableData</font><font style="background-color: white" color="black">></font></td><td style="padding-removed 25pt;"><font style="background-color: white" color="black"><TableData</font><font style="background-color: white" color="black">></font></td></tr><tr><td style="padding-removed 40pt;"><font style="background-color: white" color="black"><Parameters</font><font style="background-color: white" color="black">></font></td><td style="padding-removed 40pt;"><font style="background-color: white" color="black"><Parameters</font><font style="background-color: white" color="black">></font></td></tr><tr><td style="padding-removed 55pt;"><font style="background-color: white" color="black"><Parameter</font> <font style="background-color: white" color="black">name="Width"</font><font style="background-color: white" color="black">></font></td><td style="padding-removed 55pt;"><font style="background-color: white" color="black"><Parameter</font> <font style="background-color: white" color="black">name="Width"</font><font style="background-color: white" color="black">></font></td></tr><tr><td style="padding-removed 70pt;"><font style="background-color: white" color="black"><MinValue</font><font style="background-color: white" color="black">></font></td><td style="padding-removed 70pt;"><font style="background-color: white" color="black"><MinValue</font><font style="background-color: white" color="black">></font></td></tr><tr><td style="padding-removed 85pt;"><font style="background-color: white" color="black">10</font></td><td style="padding-removed 85pt;"><font style="background-color: white" color="black">10</font></td></tr><tr><td style="padding-removed 70pt;"><font style="background-color: white" color="black"></MinValue></font></td><td style="padding-removed 70pt;"><font style="background-color: white" color="black"></MinValue></font></td></tr><tr><td style="padding-removed 70pt;"><font style="background-color: white" color="black"><MaxValue</font><font style="background-color: white" color="black">></font></td><td style="padding-removed 70pt;"><font style="background-color: white" color="black"><MaxValue</font><font style="background-color: white" color="black">></font></td></tr><tr><td style="padding-removed 85pt;"><font style="background-color: white" color="black">100</font></td><td style="padding-removed 85pt;"><font style="background-color: white" color="black">100</font></td></tr><tr><td style="padding-removed 70pt;"><font style="background-color: white" color="black"></MaxValue></font></td><td style="padding-removed 70pt;"><font style="background-color: white" color="black"></MaxValue></font></td></tr><tr><td style="padding-removed 55pt;"><font style="background-color: white" color="black"></Parameter></font></td><td style="padding-removed 55pt;"><font style="background-color: white" color="black"></Parameter></font></td></tr><tr><td style="padding-removed 55pt;"><font style="background-color: white" color="black"><Parameter</font> <font style="background-color: white" color="black">name="Height"</font><font style="background-color: white" color="black">></font></td><td style="padding-removed 55pt;"><font style="background-color: white" color="black"><Parameter</font> <font style="background-color: white" color="black">name="Height"</font><font style="background-color: white" color="black">></font></td></tr><tr><td style="padding-removed 70pt;"><font style="background-color: white" color="black"><MinValue</font><font style="background-color: white" color="black">></font></td><td style="padding-removed 70pt;"><font style="background-color: white" color="black"><MinValue</font><font style="background-color: white" color="black">></font></td></tr><tr><td style="padding-removed 85pt;"><font style="background-color: lightgreen" color="black">40</font></td><td style="padding-removed 85pt;"><font style="background-color: lightgreen" color="black">80</font></td></tr><tr><td style="padding-removed 70pt;"><font style="background-color: white" color="black"></MinValue></font></td><td style="padding-removed 70pt;"><font style="background-color: white" color="black"></MinValue></font></td></tr><tr><td style="padding-removed 70pt;"><font style="background-color: white" color="black"><MaxValue</font><font style="background-color: white" color="black">></font></td><td style="padding-removed 70pt;"><font style="background-color: white" color="black"><MaxValue</font><font style="background-color: white" color="black">></font></td></tr><tr><td style="padding-removed 85pt;"><font style="background-color: white" color="black">600</font></td><td style="padding-removed 85pt;"><font style="background-color: white" color="black">600</font></td></tr><tr><td style="padding-removed 70pt;"><font style="background-color: white" color="black"></MaxValue></font></td><td style="padding-removed 70pt;"><font style="background-color: white" color="black"></MaxValue></font></td></tr><tr><td style="padding-removed 55pt;"><font style="background-color: white" color="black"></Parameter></font></td><td style="padding-removed 55pt;"><font style="background-color: white" color="black"></Parameter></font></td></tr><tr><td style="padding-removed 55pt;"</td><td style="padding-removed 55pt;"><font style="background-color: yellow" color="black"><Parameter</font> <font style="background-color: yellow" color="black">name="Speed"</font><font style="background-color: yellow" color="black">></font></td></tr><tr><td style="padding-removed 70pt;"</td><td style="padding-removed 70pt;"><font style="background-color: yellow" color="black"><MaxValue</font><font style="background-color: yellow" color="black">></font></td></tr><tr><td style="padding-removed 85pt;"</td><td style="padding-removed 85pt;"><font style="background-color: yellow" color="black">10</font></td></tr><tr><td style="padding-removed 70pt;"</td><td style="padding-removed 70pt;"><font style="background-color: yellow" color="black"></MaxValue></font></td></tr><tr><td style="padding-removed 70pt;"</td><td style="padding-removed 70pt;"><font style="background-color: yellow" color="black"><MinValue</font><font style="background-color: yellow" color="black">></font></td></tr><tr><td style="padding-removed 85pt;"</td><td style="padding-removed 85pt;"><font style="background-color: yellow" color="black">0</font></td></tr><tr><td style="padding-removed 70pt;"</td><td style="padding-removed 70pt;"><font style="background-color: yellow" color="black"></MinValue></font></td></tr><tr><td style="padding-removed 55pt;"</td><td style="padding-removed 55pt;"><font style="background-color: yellow" color="black"></Parameter></font></td></tr><tr><td style="padding-removed 55pt;"><font style="background-color: white" color="black"><Parameter</font> <font style="background-color: white" color="black">name="Depth"</font><font style="background-color: white" color="black">></font></td><td style="padding-removed 55pt;"><font style="background-color: white" color="black"><Parameter</font> <font style="background-color: white" color="black">name="Depth"</font><font style="background-color: white" color="black">></font></td></tr><tr><td style="padding-removed 70pt;"><font style="background-color: white" color="black"><MinValue</font><font style="background-color: white" color="black">></font></td><td style="padding-removed 70pt;"><font style="background-color: white" color="black"><MinValue</font><font style="background-color: white" color="black">></font></td></tr><tr><td style="padding-removed 85pt;"><font style="background-color: white" color="black">34</font></td><td style="padding-removed 85pt;"><font style="background-color: white" color="black">34</font></td></tr><tr><td style="padding-removed 70pt;"><font style="background-color: white" color="black"></MinValue></font></td><td style="padding-removed 70pt;"><font style="background-color: white" color="black"></MinValue></font></td></tr><tr><td style="padding-removed 70pt;"><font style="background-color: white" color="black"><MaxValue</font><font style="background-color: white" color="black">></font></td><td style="padding-removed 70pt;"><font style="background-color: white" color="black"><MaxValue</font><font style="background-color: white" color="black">></font></td></tr><tr><td style="padding-removed 85pt;"><font style="background-color: lightgreen" color="black">250</font></td><td style="padding-removed 85pt;"><font style="background-color: lightgreen" color="black">86</font></td></tr><tr><td style="padding-removed 70pt;"><font style="background-color: white" color="black"></MaxValue></font></td><td style="padding-removed 70pt;"><font style="background-color: white" color="black"></MaxValue></font></td></tr><tr><td style="padding-removed 55pt;"><font style="background-color: whi



Save out put as html file you can see missing part in that output at the end.

I need a help to correct these... Thanks in advance.
Posted
Updated 16-Dec-14 18:59pm
v3
Comments
DamithSL 17-Dec-14 0:26am    
you better include small xml input samples and what is expected output
Varun_Kumar 17-Dec-14 1:00am    
me added input values

1 solution

this can be solve by below steps,


C#
string FindXMLDiff(string oldXmlString, string newXmlString, out string xmlDiff)
        {
            string htmlOut = string.Empty; 
            xmlDiff = "";

            System.IO.StringReader oldString = new System.IO.StringReader(oldXmlString);
            System.IO.StringReader newString = new System.IO.StringReader(newXmlString);
            System.Xml.XmlReader oldEReader = System.Xml.XmlReader.Create(oldString);
            System.Xml.XmlReader newEReader = System.Xml.XmlReader.Create(newString);

            XmlDiffView dv = new XmlDiffView();
            using (MemoryStream diffFileStream = new MemoryStream())
            {
                XmlTextWriter tw = new XmlTextWriter(new StreamWriter(diffFileStream));
                tw.Formatting = Formatting.Indented;

                XmlDiff diff = new XmlDiff(XmlDiffOptions.IgnoreChildOrder |
                                                     XmlDiffOptions.IgnoreNamespaces |
                                                     XmlDiffOptions.IgnorePrefixes |
                                                     XmlDiffOptions.IgnoreDtd |
                                                     XmlDiffOptions.IgnoreWhitespace);
                bool isEqual = false;

                //Now compare the two files.
                try
                {
                    isEqual = diff.Compare(oldEReader, newEReader, tw);
                }
                catch (XmlException xe)
                {
                }
                finally
                {
                    tw.Flush();
                    diffFileStream.Position = 0;
                }

                //Load the original file again and the diff file.
                System.Xml.XmlTextReader orgReader = new System.Xml.XmlTextReader(new System.IO.StringReader(oldXmlString));
                XmlTextReader diffGram = new XmlTextReader(diffFileStream);
                dv.Load(orgReader,
                    diffGram);

                orgReader.Close();
                diffGram.Close();

            }
            using (MemoryStream htmlStream = new MemoryStream())
            {
                StreamWriter sw1 = new StreamWriter(htmlStream);

                sw1.Write("<html><body><table style="font-family:verdana; font-size:12">");
                sw1.Write("<tr><td>Legend: <font style="background-color: yellow" hold=" />                " color="black">added</font>  <font style="background-color: red" hold=" />                " color="black">removed</font>  <font style="background-color: " +<br mode="hold" />                "lightgreen" color="black">changed</font>" +
                "</td><td><font style="background-color: red" color="blue">moved from</font>" +
                "  <font style="background-color: yellow" color="blue">moved to" +
                "</font>  <font style="background-color: white" color="#AAAAAA">" + "ignored</font></td></tr>");
                sw1.Write("<tr><td>Old");
                sw1.Write("</td><td>New");
                sw1.Write("</td></tr>");

                dv.GetHtml(sw1);

                sw1.Write("</table></body></html>");
                sw1.Flush();
                htmlStream.Position = 0;

                using (StreamReader sr = new StreamReader(htmlStream))
                {
                    htmlOut = sr.ReadToEnd();
                }
            }

            dv = null;
            oldString.Close();
            newString.Close();
            oldEReader.Close();
            newEReader.Close();
            return htmlOut;
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900