Monday, July 15, 2013

how to compare two xml files in asp.net using c#

Title:How to compare XML files in Asp.net using C#

Description:

Now i would like to give an examples on Comparing of XML documents using C# code.Asp.net provide an algorithm which is "hash algorithm",can be used for encryption.So based on those methods i will do the comparison of files In the below method i have use two parameters which indicates the path of XML files

Examples:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Security.Cryptography;

private void CompareXMLDocumnet(string ActualDocumnet,string GenratedDocument)
{
if (File.Exists(ActualDocumnet) && File.Exists(GenratedDocument))
{
HashAlgorithm HA = HashAlgorithm.Create();
FileStream XmlFstream1 = new FileStream(ActualDocumnet, FileMode.Open);
FileStream XmlFstream2 = new FileStream(GenratedDocument, FileMode.Open);
byte[] Actual_Xmlhash1;
byte[] Genrated_Xmlhash2;
Actual_Xmlhash1= HA.ComputeHash(XmlFstream1);
Genrated_Xmlhash2= HA.ComputeHash(XmlFstream2);
XmlFstream1.Close();
XmlFstream2.Close();
if (Convert.ToBase64String(Actual_Xmlhash1) == Convert.ToBase64String(Genrated_Xmlhash2))
{
Response.Write(" similar="">
}
else
{
throw new Exception("Files are different");
}
}
}

No comments:

Bel