Wednesday, September 28, 2005

Using CompareHTML to compare postings

CompareHTML is a component which ships with MCMS which can be used to compare two postings. This blog explains how it can be used to compare two postings. It can be extended as needed.

Add a reference to CompareHTML.dll. Also your class must have reference to System.Net, System.IO, and COMPAREHTMLLib.

The code piece given below contains the part which can be used to compare two postings. As it is simple and self explanatory, I’m not going to walk through that.

//Web client is used to get the content from the server
using(WebClient webClient = new WebClient())
{
NetworkCredential networkCredential = new NetworkCredential("username","password","domain");
webClient.Credentials = networkCredential;
string firstPosting = "";
string secondPosting = "";
using(Stream networkStream = webClient.OpenRead("first posting's url"))
{
using(StreamReader streamReader = new StreamReader(networkStream))
{
firstPosting = streamReader.ReadToEnd();
}
}
using(Stream networkStream = webClient.OpenRead("second posting's url"))
{
using(StreamReader streamReader = new StreamReader(networkStream))
{
secondPosting = streamReader.ReadToEnd();
}
}
//This is from CompareHTML.dll, which will compare both the content.
Compare postingCompare = new CompareClass();
string comparedPosting = postingCompare.CompareBuffers(firstPosting,secondPosting,false);
string pathDiffFile = "a place to save the compared html file";
using(StreamWriter streamWriter = new StreamWriter(pathDiffFile))
{
streamWriter.Write(comparedPosting);
}
//This is to open the created html file.
System.Diagnostics.Process.Start(pathDiffFile);
}

Note : As CompareHTML.dll is not documented, take precautions before using it as it may change at any stage without any notification. Also reverse engineering CompareHTML is a license violation!

Labels: , ,

0 Comments:

Post a Comment

<< Home