现在的位置: 首页 > 综合 > 正文

在.net中比较XML

2013年02月28日 ⁄ 综合 ⁄ 共 2827字 ⁄ 字号 评论关闭

    本文翻写自我早年的一篇日志http://www.cnblogs.com/ols/archive/2008/05/02/1179849.html

    早年的那篇日志,主要是讲我的探索过程,而如何在自己的.net程序中加入比较XML的功能,讲得比较少,这里尝试讲一下如何加入这一功能。如果有读者想自己研究,可以参考我原来的那篇日志。

    先来看一下比较XML的效果图

XmlDiff

    第一步:准备开发环境,VS2005或更高版本。

    第二步:下载XML notepad 2007,并安装。

    第三步:在XML notepad 2007安装目录下找到xmldiffpatch.dll和XmlDiffPatch.View.dll这两个文件,备用(想起菜谱上的“洗净切丝,备用”)。

    第四步:在自己的.net项目(project)中添加引用,xmldiffpatch.dll和XmlDiffPatch.View.dll,如下图所示

project_ref

    第五步:下载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>&nbsp;&nbsp;<font style='background-color: red'"+
 
" color='black'>removed</font>&nbsp;&nbsp;<font style='background-color: "+
 
"lightgreen' color='black'>changed</font>&nbsp;&nbsp;"+
 
"<font style='background-color: red' color='blue'>moved from</font>"+
 
"&nbsp;&nbsp;<font style='background-color: yellow' color='blue'>moved to"+
 
"</font>&nbsp;&nbsp;<font style='background-color: white' color='#AAAAAA'>"+
 
"ignored</font></td></tr>");

       替换成下面的代码

 

代码

private static readonly string htmlHeader = "<html><head>"
    + "<style type=\"text/css\"><!--"   //css
    + ".add {color: black;background-color: yellow;}"   //add               
    + ".remove {color: black;background-color: red;}"//remove
    + ".change {color: black;background-color: lightgreen;}"//change
    + ".movefrom {color: blue;background-color: red;}"//movefrom
    + ".moveto {color: blue;background-color: yellow;}"//ignore
    + ".ignore {color=\"#AAAAAA\";background-color: white;}"//moveto
    + "--></style>"
    + "<title>comparision</title></head><body><table width='100%'>"//Write Title
    //Write Legend.
    + "<tr><td colspan='2' align='center'><b>Legend:</b>"
    + "<font style='background-color: yellow' color='black'>added</font>&nbsp;&nbsp;"
    + "<font style='background-color: red' color='black'>removed</font>&nbsp;&nbsp;"
    + "<font style='background-color: lightgreen' color='black'>changed</font>&nbsp;&nbsp;"
    + "<font style='background-color: red' color='blue'>moved from</font>&nbsp;&nbsp;"
    + "<font style='background-color: yellow' color='blue'>moved to</font>&nbsp;&nbsp;"
    + "<font style='background-color: white' color='#AAAAAA'>ignored</font>"
    + "</td></tr>";

sw1.Write(htmlHeader);

 

    第八步:对VisualXmlDiff.cs文件的其他修改,比如将比较结果保存为永久性的文件,而不是程序退出时就消失的临时文件。这里就不具体讲了,看附件中的代码吧。

 

    附件说明:附件是一个演示用的vs2005工程,功能很简单,就是选择2个xml文件,设置比较选项,将比较结果显示在界面上。本文开头的图片就是运行界面的截图。

 

    顺便说一下,写这篇日志时,随手搜了一下,发现一篇转载的文章点击量比原文高很多,这真是一件有意思的事情,转载的地址http://hi.baidu.com/zhengjianweilx/blog/item/9ce92ade18b9965fcdbf1a47.html

 

参考资料

XML notepad 2007

XML Notepad 2006设计

XML差异与修补工具

 

TrackBack :http://www.cnblogs.com/ols/archive/2011/02/16/1955832.html

 

    最后,为了便于网友搜到这篇日志,写几句废话“C#比较XML文件”,“VB.NET比较XML文件”,“程序中加入比较XML的功能”,“程序中比较XML”。

抱歉!评论已关闭.