Question

In: Computer Science

# Compare two Xml_ file in C# and extract the difference between them in other xml_file...

# Compare two Xml_ file in C# and extract the difference between them in other xml_file

Hi!

I have problem about xml_file, I need to compare between two xml_file with deferent values and I must extract the deference in a new xml file in C#, .Net, I need to write program(not Microsoft XmlDiff and Patch tools). If you can help me please!

Thank you!

Original file is the first one!

<root>

<data name="senChangePassword" xml:space="preserve">

<value>Hi</value>

<comment> Jessica</comment>

</data>

<data name="senChangesWereSuccessfullySaved" xml:space="preserve">

<value>save change.</value>

<comment> Jessica</comment>

</data>

<data name="senChangeUserSettings" xml:space="preserve">

<value>change data</value>

<comment> Jessica</comment>

</data>

<data name="senCompareWith" xml:space="preserve">

<value>Compare</value>

<comment>Jessica</comment>

</data>

<data name="senPasswordResetMailText" xml:space="preserve">

<value>Here projectportal.&lt;br/&gt;&lt;br/&gt;[password]&lt;br/&gt;&lt;br/&gt;go to projectportal: [link]&lt;br/&gt;&lt;br/&gt;Thankyou&lt;br/&gt;&lt;/br&gt;[manager]</value>

<comment>09-2019</comment>

</data>

<data name="senCreatedQuestions" xml:space="preserve">

<value>Save</value>

<comment>Jessica</comment>

</data>

</root>

===========================================

The second xlm_file

<?xml version="1.0" encoding="UTF-8" ?>

<root>

<data name="senChangePassword" xml:space="preserve">

<value>Change Password</value>

<comment> Jessica</comment>

</data>

<data name="senChangeUserSettings" xml:space="preserve">

<value>Change data</value>

<comment>Jessica</comment>

</data>

<data name="senCompareWith" xml:space="preserve">

<value>Compare</value>

<comment> Jessica</comment>

</data>

<data name="senPasswordResetMailText" xml:space="preserve">

<value>Here you get a new password for the project portal.&lt;br/&gt;&lt;br/&gt;[password]&lt;br/&gt;&lt;br/&gt;Follow this link to the project portal: [link]&lt;br/&gt;&lt;br/&gt;Sincerely&lt;br/&gt;&lt;/br&gt;[manager]</value>

<comment> 09-2019</comment>

</data>

<data name="senCreatedQuestions" xml:space="preserve">

<value>Created questions</value>

<comment> Jessica</comment>

</data>

</root>

======================================================
 


Solutions

Expert Solution

You can use the following code to compare two XML files and write the difference in new XML file which is as follow :

public void CompareXmlFile(string file1, string file2, string diffFileNameWithPath)
        {

            XmlReader reader1 = XmlReader.Create(new StringReader(file1));
            XmlReader reader2 = XmlReader.Create(new StringReader(file2));

            StringBuilder differenceStringBuilder = new StringBuilder();

            using (FileStream fs = new FileStream(diffFileNameWithPath, FileMode.Create))
            {
                XmlWriter diffGramWriter = XmlWriter.Create(fs);

                XmlDiff xmldiff = new XmlDiff(XmlDiffOptions.IgnoreChildOrder |
                                        XmlDiffOptions.IgnoreNamespaces |
                                        XmlDiffOptions.IgnorePrefixes);
                bool bIdentical = xmldiff.Compare(file1, file2, false, diffGramWriter);

                diffGramWriter.Close();
            }
        }  

Calling Code:

CompareXmlFile(@"C:\XML\Source.xml", @"C:\XML\changed.xml", @"C:\Diff.xml");

Hope this will help you.


Related Solutions

difference between vanilla extract and artificial vanilla
difference between vanilla extract and artificial vanilla
What’s the difference between switchgear and smart switchgear? Please compare them in a table if you...
What’s the difference between switchgear and smart switchgear? Please compare them in a table if you can
Indicate the two types of comparative studies, and indicate the difference between them.
Indicate the two types of comparative studies, and indicate the difference between them.
Explain the difference between the two circuits, C-M-C and M-C-M.
Explain the difference between the two circuits, C-M-C and M-C-M.
Compare the two systems of classifying reactions – what similarities and differences are there between them?...
Compare the two systems of classifying reactions – what similarities and differences are there between them? What factors are used to determine what a reaction is classified as according to what atoms do? What different classifications fall under this category? What factors are used to determine what a reaction is classified as according to type? What different classifications fall under this category? (Nature of each classification method, what each focuses on to classify reactions, what similarities and differences exist, and...
Currency options. What are the two types, and what is the difference between them? What is...
Currency options. What are the two types, and what is the difference between them? What is the break-even point for each type? Conceptually understand and be able to mathematically work with them. A. You purchase a put option on the Canadian dollar with a strike price of $0.91 and a premium of $0.02. If before expiration the spot rate of the Canadian dollar is $0.92, will you exercise the option? b. What if the spot rate before expiration is $0.88?...
C++ Programming Simply explain what the difference between "including" a header file and using a compiled...
C++ Programming Simply explain what the difference between "including" a header file and using a compiled library file during linking? Where are the C++ header files and compiled C++ library files on your computer?
what is the difference between a normal file and a Webdocument?
what is the difference between a normal file and a Web document?
characteristics of MOSFET and BIPOLAR and the difference between them
characteristics of MOSFET and BIPOLAR and the difference between them
Please compare the mean and the median. What is the main difference between the two? Please...
Please compare the mean and the median. What is the main difference between the two? Please construct an example of when it would be helpful to calculate the mean, and another example illustrating when it would be helpful to calculate the median. Posts should be approximately 100 words.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT