本文翻写自我早年的一篇日志http://www.cnblogs.com/ols/archive/2008/05/02/1179849.html。
早年的那篇日志,主要是讲我的探索过程,而如何在自己的.net程序中加入比较XML的功能,讲得比较少,这里尝试讲一下如何加入这一功能。如果有读者想自己研究,可以参考我原来的那篇日志。
先来看一下比较XML的效果图
第一步:准备开发环境,VS2005或更高版本。
第二步:下载XML notepad 2007,并安装。
第三步:在XML notepad 2007安装目录下找到xmldiffpatch.dll和XmlDiffPatch.View.dll这两个文件,备用(想起菜谱上的“洗净切丝,备用”)。
第四步:在自己的.net项目(project)中添加引用,xmldiffpatch.dll和XmlDiffPatch.View.dll,如下图所示
第五步:下载XML差异与修补工具,打开代码,找到VisualXmlDiff.cs文件,备用。
第六步:将VisualXmlDiff.cs和VisualXmlDiff.resx添加到项目中。
第七步:修改VisualXmlDiff.cs文件,主要是解决比较结果的着色问题,找到下面的代码
sw1.Write("<html><body><table width='100%'>");
//Write Legend.
sw1.Write("<tr><td colspan='2' align='center'><b>Legend:</b> <font style='background-color: yellow'" +
" color='black'>added</font> <font style='background-color: red'"+
" color='black'>removed</font> <font style='background-color: "+
"lightgreen' color='black'>changed</font> "+
"<font style='background-color: red' color='blue'>moved from</font>"+
" <font style='background-color: yellow' color='blue'>moved to"+
"</font> <font style='background-color: white' color='#AAAAAA'>"+
"ignored</font></td></tr>");
//Write Legend.
sw1.Write("<tr><td colspan='2' align='center'><b>Legend:</b> <font style='background-color: yellow'" +
" color='black'>added</font> <font style='background-color: red'"+
" color='black'>removed</font> <font style='background-color: "+
"lightgreen' color='black'>changed</font> "+
"<font style='background-color: red' color='blue'>moved from</font>"+
" <font style='background-color: yellow' color='blue'>moved to"+
"</font> <font style='background-color: white' color='#AAAAAA'>"+
"ignored</font></td></tr>");