在.net中比較XML

來源:互聯網
上載者:User

    本文翻寫自我早年的一篇日誌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>&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”。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.