In: Computer Science
# 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.<br/><br/>[password]<br/><br/>go to projectportal: [link]<br/><br/>Thankyou<br/></br>[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.<br/><br/>[password]<br/><br/>Follow this link to the project portal: [link]<br/><br/>Sincerely<br/></br>[manager]</value>
<comment> 09-2019</comment>
</data>
<data name="senCreatedQuestions" xml:space="preserve">
<value>Created questions</value>
<comment> Jessica</comment>
</data>
</root>
======================================================
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.