Click here to Skip to main content
15,879,090 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
hello experts i guess there was a bit confusion earlier as i was not clear with my specifications.
my aim is to only find a difference between 2 large xml files and print its difference into the 3rd xml file and in xml format.
in this case there are 'n' number of parent nodes and child nodes which does not changes only child nodes values which is printed in CData may vary.
i need to print only difference between this 2 xml files and the values inside nodes which are same need not be printed.

like example :
test1.xml :
XML
<?xml version="1.0" encoding="utf-8"?>
<wpassetd>
  <Main>
    <data>
      <machinename><![CDATA[zomy]]></machinename>
      <scandate><![CDATA[03/23/2014 10:56:34]]></scandate>
      <guid><![CDATA[31c0841e-f7bf-4de1-9d75-7e9080498e6b-20141216020243430495]]></guid>
      <regid><![CDATA[2853611]]></regid>
    </data>
  </Main>
</wpassetd>

test2.xml:

<?xml version="1.0" encoding="utf-8"?>
<wpassetd>
  <Main>
    <data>
      <machinename><![CDATA[zomy]]></machinename>
      <scandate><![CDATA[03/23/2015 18:56:34]]></scandate>
      <guid><![CDATA[31c0841e-f7bf-4de1-9d75-7e9080498e6b-20141216020243430495]]></guid>
      <regid><![CDATA[2853622]]></regid>
    </data>
  </Main>
</wpassetd>


output.xml should be difference of test1.xml and test2.xml:

<?xml version="1.0" encoding="utf-8"?>
<wpassetd>
  <Main>
    <data>
       <scandate><![CDATA[03/23/2015 18:56:34]]></scandate>
       <regid><![CDATA[2853622]]></regid>
    </data>
  </Main>
</wpassetd>




using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml.Linq;


namespace ConsoleApplication15
{
    class Program
    {
        static void Main(string[] args)
        {

           

            var doc1 = XDocument.Load(@"tanu1.xml");
            var doc2 = XDocument.Load(@"tanu2.xml");


            var dict = doc1.Root.Elements("parent").ToDictionary(el =>
                el.Attribute("children").Value);

            doc2.Root.Elements("parent").ToList().ForEach(el =>
            {
                XElement el2;
                if (dict.TryGetValue(el.Attribute("children").Value, out el2) &&
                    !el.Attributes().Select(a => new { a.Name, a.Value }).Except(
                    el2.Attributes().Select(a => new { a.Name, a.Value })).Any())
                    el.Remove();
            });
            doc2.Save(@"file3.xml");
        }
       
    }
}


it doesnot work well or i think i am somewhere wrong in accepting values as i am not that good coder. 
does any one has alternate to this?
Posted
Updated 7-Apr-15 0:52am
v12
Comments
Sergey Alexandrovich Kryukov 6-Apr-15 2:45am    
You need to define what is "difference between two XML files". Do you think it is obvious? No, it is not trivial and does not have some universal predefined meaning. It can be clearly illustrated. So, this is not a problem, because you goal is not formulated.
—SA
Member 11460314 6-Apr-15 9:45am    
thanks Sascha Lefevre for making me alert about this as i was unaware.

Hey Sergey, basically from above example it defines thatin test1.xml there was a software named "C# to C++ Converter (Free Edition)" installed onto the system but in test2.xml file the same is missing. henceforth if present in test1.xml but missing in test2.xml then we must consider that the software is uninstalled from the system.
similarly in test1.xml there is no yahoomessenger but in test2.xml there seems to be yahoo messenger. henceforth we consider that software present in test2.xml but not in test1.xml is newly installed software onto the system.
Hope i made all things clear now
Sergey Alexandrovich Kryukov 6-Apr-15 10:02am    
No. You need too map three things: two input files and one output. The concept of "difference" is ambiguous. For example, two items are swapped two in a second file — how output will look like? Now, if you know, remember this output, output1. Create another input file, order of the items is different, but items are the same. New output, output2. Will it be the same as output1. If it will, you don't put all "differences" into account. Think about it.
You need exact specification, and it will be the major part of the problem.
—SA
Sascha Lefèvre 6-Apr-15 9:23am    
Please note you commented on your question rather than replying to Sergey Alexandrovich Kryukov's comment - so he didn't get a notification about your comment. Click the "Reply"-button next to the name of the commenting member to reply to his comment.
Afzaal Ahmad Zeeshan 7-Apr-15 4:17am    
Please, wait for a moment. Go to MSDN, look for XML serializer, convert the XML into an object (deserialize), read properties (nodes), compare them and then create another object for the differences and then serialize it into XML.

This is the answer to your question.

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