菜鳥學堂:
在前兩篇文章中我們討論了xml文件的讀取和寫入,但都是基于流模型的解決方案,今天我們就來談談在c#中如何實現dom,dom確實有它的不足,但在編程工作中它還是不可或缺的技術。下面我們來簡單了解一下dom的相關知識。
dom的全稱是document object model(文檔對象模型),它是來自w3c的官方標準,它允許按照w3c標準w3c dom level1和w3c dom level2的規范所定義的規則,通過編程來讀取,操縱和修改xml文檔。dom的工作方式是:首先將xml文檔一次性的裝入內存,然后根據文檔中定義的元素和屬性在內存中創建一個“樹型結構”也就是一個文檔對象模型,這里的含義其實是把文檔對象化,文檔中每個節點對應著模型中一個對象,而我們都知道對象提供編程接口,所以在application中我們正是使用這組對象來訪問xml文檔進而操作xml文檔,下圖闡述了application和dom交互的過程:
dom既然是在內存中創建樹型結構視圖進而提供編程接口,那我們就以下面這個xml片段來說明dom是如何創建樹型結構的:
<parent>
<child id=”123”>text here</child>
</parent>
如果用dom加載以上文檔,它將在內存中創建的樹型結構如下圖:
dom的關鍵在于它允許直接更新內存中的樹型結構,而不必重定向到其他輸出,因此,添加、更新或刪除結構中信息的操作效率更高。而作為程序員的我們重要的是要了解dom所提供的編程接口以實現對xml文檔進行操作,事實上,.net framework定義了一組類用于反映dom的體系結構,下面來看一下.net dom的繼承結構:
在上圖中所有弧角矩形中所包含的類描述了所有可能在xml文檔中出現的節點類型,而操作xml文檔不外乎是操作其中的節點,這些類又都是從xmlnode類派生而來,所以我們今天的主題是討論xmlnode類和它的子類xmldocument,下面對這些類做簡單的介紹:
xmlnode類:
該類是dom中所有其他節點的抽象基類,它定義所有在更低級的類中繼承或重寫的成員。它表示xml文檔中的單一節點,它提供了用于導航dom樹型結構的基本方法和屬性,使用xmlnodetype枚舉器可以枚舉其下的所有節點類型。以下介紹該類的部分屬性和方法:
屬性:
[c#]
public virtual bool haschildnodes {get;} 獲取一個值,該值指示當前節點是否有任何子節點
public virtual xmlnodelist childnodes {get;} 獲取當前節點的所有子節點
public virtual xmlnode firstchild {get;} 獲取當前節點的第一個子級
public virtual xmlnode lastchild {get;} 獲取當前節點的最后一個子級
public virtual xmlnode parentnode {get;} 獲取當前節點的父級
public virtual xmlnode nextsibling {get;} 獲取當前節點的下一個兄弟節點
public virtual xmlnode previoussibling {get;} 獲取當前節點的上一個兄弟節點
public virtual string innertext {get; set;} 獲取或設置當前節點及其所有子節點的文本內容的串聯值
public virtual string innerxml {get; set;} 獲取或設置僅代表當前節點的子節點的標記
public virtual string outerxml {get;} 獲取表示當前節點及其所有子節點的標記
方法:
public xmlnodelist selectnodes(string); 選擇文檔中匹配 xpath 表達式的節點列表
public xmlnode selectsinglenode(string); 選擇文檔中匹配 xpath 表達式的第一個 xmlnode
public virtual xmlnode appendchild(xmlnode newchild) 將指定的節點添加到該節點的子節點列表的末尾
public virtual xmlnode prependchild(xmlnode newchild) 將指定的節點添加到該節點的子節點列表的開頭
public virtual xmlnode removechild(xmlnode oldchild) 移除指定的子節點
public virtual xmlnode replacechild(xmlnode newchild,xmlnode oldchild) 用 newchild 節點替換子節點 oldchild
xmlnodelist類:
該類表示xmlnode的有序集合,它有以下常用成員:
count——以整數形式返回xmlnodelist中的節點數
itemof——搜索在指定索引處的節點
getenumerator()——提供迭代遍歷節點列表的foreach樣式
item()——返回參數指定的索引處的節點
xmldocument類:
xmldocument類是xml文檔的.net表示形式,它代表了內存中樹型結構的文檔節點(所有的節點都在文檔節點下),xmldocument類包含所有的createxxx()方法,這些方法允許創建所有派生自xmlnode的類型的節點,通常將該類與xmlnode類一起使用以完成對文檔的操作,該類有一個load()方法用于加載xml文檔,該方法的一個重載版本允許從xmltextreader加載文檔,這給我們帶來的好處是在操作較大的文檔時我們可以先使用xmltextreader過濾不相關的文檔部分,這樣即解決了dom所帶來的資源損耗問題又可以保留dom對文檔操控的便利性,該類的save()方法用于保存文檔。
接下來用一個簡單的例子來說明在c#中如何實現dom,照舊看代碼前先看下運行效果圖:
loadxml按紐用于加載xml文檔,loadxmlreader按紐使用xmltextreader加載文檔,savexml按紐保存文檔,savexmlwriter按紐將文檔保存到xmltextwriter中,add product按紐添加節點,replace product按紐替換節點,change order按紐修改文檔,remove product info按紐移除節點。
domoperation類封裝了所有按紐功能的實現,代碼如下:
namespace domsamples
{
using system;
using system.xml;
using system.text;
using system.windows.forms;
using system.componentmodel;
/// <summary>
/// domoperation 提供對xml文件操作的類
/// </summary>
/// <remarks>
/// 該類用于提供對xml文件進行一般的操作,如(添加,刪除,替換節點,加載,保存文件)等,該類內部實現采用dom技術。
/// </remarks>
public class domoperation : idisposable
{
private string _xmlpath;
private xmldocument xmldoc;
#region domoperation 構造器
/// <summary>
/// 默認構造器,對該類成員進行默認賦值
/// </summary>
public domoperation()
{
this._xmlpath = string.empty;
this.xmldoc = null;
}
/// <summary>
/// 帶參構造器,對該類成員進行初始賦值
/// </summary>
/// <param name="xmlpath">xml文件路徑</param>
public domoperation(string xmlpath)
{
this._xmlpath = xmlpath;
this.xmldoc = null;
}
#endregion
#region domoperation 資源釋放方法
/// <summary>
/// 清理該對象所有正在使用的資源
/// </summary>
public void dispose()
{
this.dispose(true);
gc.suppressfinalize(this);
}
/// <summary>
/// 釋放該對象的實例變量
/// </summary>
/// <param name="disposing"></param>
protected virtual void dispose(bool disposing)
{
if (!disposing)
return;
if (this._xmlpath != null)
{
this._xmlpath = null;
}
if (this.xmldoc != null)
{
this.xmldoc = null;
}
}
#endregion
#region domoperation 屬性
/// <summary>
/// 獲取或設置xml文件的路徑
/// </summary>
public string xmlpath
{
get
{
return _xmlpath;
}
set
{
this._xmlpath = value;
}
}
#endregion
/// <summary>
/// 加載xml文件
/// </summary>
/// <remarks>
/// 該方法使用xml文件的路徑加載xml文件并返回整個xml文件的內容
/// </remarks>
/// <returns>整個xml文件的內容</returns>
public string load()
{
xmldoc = new xmldocument();
try
{
xmldoc.load(this._xmlpath);
}
catch(xmlexception xmlexp)
{
throw new xmlexception(xmlexp.tostring());
}
return xmldoc.outerxml;
}
/// <summary>
/// 加載xml文件
/// </summary>
/// <remarks>
/// 該方法使用xmlreader在xml文檔中定位并加載xml文件最后返回xml文件的內容
/// </remarks>
/// <param name="nodetype">將要定位的節點類型</param>
/// <param name="localname">將要定位的節點名稱</param>
/// <returns>xml文件的內容</returns>
public string loadbyxmlreader(xmlnodetype nodetype, string localname)
{
string xmlstr = string.empty;
xmltextreader xmltxtrd = new xmltextreader(this._xmlpath);
xmldoc = new xmldocument();
try
{
// 在文檔中定位
while(xmltxtrd.read())
{
if (xmltxtrd.nodetype == nodetype)
if(xmltxtrd.localname == localname)
break;
}
xmldoc.load(xmltxtrd);
xmltxtrd.close();
xmlstr += "===== outerxml =====";
xmlstr += system.environment.newline;
xmlstr += xmldoc.firstchild.outerxml;
xmlstr += system.environment.newline;
xmlstr += system.environment.newline;
xmlstr += "===== innerxml =====";
xmlstr += system.environment.newline;
xmlstr += xmldoc.firstchild.innerxml;
xmlstr += system.environment.newline;
xmlstr += system.environment.newline;
xmlstr += "===== innertext =====";
xmlstr += system.environment.newline;
xmlstr += xmldoc.firstchild.innertext;
xmlstr += system.environment.newline;
xmlstr += system.environment.newline;
xmlstr += "===== value =====";
xmlstr += system.environment.newline;
xmlstr += xmldoc.firstchild.childnodes[0].childnodes[0].value + " " +
xmldoc.firstchild.childnodes[1].childnodes[0].value + " " +
xmldoc.firstchild.childnodes[2].childnodes[0].value;
xmlstr += system.environment.newline;
xmlstr += system.environment.newline;
}
catch(xmlexception xmlexp)
{
throw new xmlexception(xmlexp.tostring());
}
finally
{
if (xmltxtrd != null && xmltxtrd.readstate != readstate.closed)
xmltxtrd.close();
}
return xmlstr;
}
/// <summary>
/// 保存xml文件
/// </summary>
/// <remarks>
/// 該方法將傳入的xml文本保存在傳入的路徑中最后返回xml文件的內容
/// </remarks>
/// <param name="xmltext">xml文本</param>
/// <param name="savepath">保存路徑</param>
/// <returns>xml文件的內容</returns>
public string save(string xmltext, string savepath)
{
xmldoc = new xmldocument();
try
{
xmldoc.loadxml(xmltext);
xmldoc.save(savepath);
xmldoc.load(savepath);
}
catch(xmlexception xmlexp)
{
throw new xmlexception(xmlexp.tostring());
}
return xmldoc.outerxml;
}
/// <summary>
/// 保存xml文件
/// </summary>
/// <remarks>
/// 該方法將傳入的xml文本保存在xmltextwriter編寫器中并使用傳入的路徑構造該編寫器最后返回xml文件的內容
/// </remarks>
/// <param name="xmltext">xml文本</param>
/// <param name="savepath">保存路徑</param>
/// <returns>xml文件的內容</returns>
public string savebyxmlwriter(string xmltext, string savepath)
{
xmltextwriter xmltxtwt = new xmltextwriter(savepath,encoding.unicode);
xmldoc = new xmldocument();
try
{
xmldoc.loadxml(xmltext);
xmldoc.save(xmltxtwt);
xmltxtwt.close();
xmldoc.load(savepath);
}
catch(xmlexception xmlexp)
{
throw new xmlexception(xmlexp.tostring());
}
finally
{
if (xmltxtwt != null && xmltxtwt.writestate != writestate.closed)
xmltxtwt.close();
}
return xmldoc.outerxml;
}
/// <summary>
/// 添加子節點
/// </summary>
/// <remarks>
/// 該方法利用傳入的父節點路徑選擇該節點并將傳入的xml文檔片段添加到該父節點下最后返回添加后xml文件的內容
/// </remarks>
/// <param name="xmldoc"></param>
/// <param name="xmldocfrag">xml文檔片段</param>
/// <param name="parentnodepath">父節點路徑</param>
/// <returns>添加后xml文件的內容</returns>
public string addchildnode(xmldocument xmldoc, xmldocumentfragment xmldocfrag, string parentnodepath)
{
this.xmldoc = xmldoc;
xmlelement selectele = (xmlelement)xmldoc.selectsinglenode(parentnodepath);
selectele.appendchild(xmldocfrag.firstchild);
return this.xmldoc.outerxml;
}
/// <summary>
/// 替換子節點
/// </summary>
/// <remarks>
/// 該方法利用父節點路徑選擇該節點并用傳入的xml文檔片段替換該父節點下的第i個子節點最后返回替換后的xml文件的內容
/// </remarks>
/// <param name="xmldoc"></param>
/// <param name="xmldocfrag">xml文檔片段</param>
/// <param name="parentnodepath">父節點路徑</param>
/// <param name="i">第i個子節點</param>
/// <returns>替換后的xml文件的內容</returns>
public string replacechildnode(xmldocument xmldoc, xmldocumentfragment xmldocfrag, string parentnodepath, int i)
{
this.xmldoc = xmldoc;
xmlelement selectele = (xmlelement)xmldoc.selectsinglenode(parentnodepath);
xmlelement childele = (xmlelement)selectele.childnodes[i];
selectele.replacechild(xmldocfrag.firstchild,childele);
return this.xmldoc.outerxml;
}
/// <summary>
/// 移除子節點
/// </summary>
/// <remarks>
/// 該方法利用傳入的父節點名稱選擇該父節點并利用傳入的子節點名稱選擇該子節點選定后使用父節點的removechild方法移除子節點
/// 最后返回移除后xml的文件內容
/// </remarks>
/// <param name="parentnodename">父節點名稱</param>
/// <param name="childnodename">子節點名稱</param>
/// <returns>移除后xml的文件內容</returns>
public string removechildnode(string parentnodename, string childnodename)
{
xmldoc = new xmldocument();
xmldoc.load(this._xmlpath);
xmlnodelist parentnodelist = xmldoc.getelementsbytagname(parentnodename);
foreach (xmlnode parentnode in parentnodelist)
{
xmlnodelist childnodelist = parentnode.selectnodes(childnodename);
foreach (xmlnode childnode in childnodelist)
{
parentnode.removechild(childnode);
}
}
return xmldoc.outerxml;
}
}
}
窗口程序代碼如下:
namespace domsamples
{
using system;
using system.xml;
using system.text;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.windows.forms;
using system.data;
/// <summary>
/// form1 的摘要說明。
/// </summary>
public class form1 : system.windows.forms.form
{
private system.windows.forms.textbox textbox1;
private system.windows.forms.button button1;
private system.windows.forms.button button2;
private system.windows.forms.button button3;
private system.windows.forms.button button4;
private system.windows.forms.button button5;
private system.windows.forms.button button6;
private system.windows.forms.button button7;
private system.windows.forms.button button8;
private string xmlpath;
/// <summary>
/// 必需的設計器變量。
/// </summary>
private system.componentmodel.container components = null;
public form1()
{
//
// windows 窗體設計器支持所必需的
//
initializecomponent();
//
// todo: 在 initializecomponent 調用后添加任何構造函數代碼
//
}
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
protected override void dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.dispose();
}
}
base.dispose( disposing );
}
#region windows 窗體設計器生成的代碼
/// <summary>
/// 設計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內容。
/// </summary>
private void initializecomponent()
{
this.textbox1 = new system.windows.forms.textbox();
this.button1 = new system.windows.forms.button();
this.button2 = new system.windows.forms.button();
this.button3 = new system.windows.forms.button();
this.button4 = new system.windows.forms.button();
this.button5 = new system.windows.forms.button();
this.button6 = new system.windows.forms.button();
this.button7 = new system.windows.forms.button();
this.button8 = new system.windows.forms.button();
this.suspendlayout();
//
// textbox1
//
this.textbox1.anchor = ((system.windows.forms.anchorstyles)((((system.windows.forms.anchorstyles.top | system.windows.forms.anchorstyles.bottom)
| system.windows.forms.anchorstyles.left)
| system.windows.forms.anchorstyles.right)));
this.textbox1.location = new system.drawing.point(8, 8);
this.textbox1.multiline = true;
this.textbox1.name = "textbox1";
this.textbox1.scrollbars = system.windows.forms.scrollbars.both;
this.textbox1.size = new system.drawing.size(776, 296);
this.textbox1.tabindex = 0;
this.textbox1.text = "";
//
// button1
//
this.button1.anchor = ((system.windows.forms.anchorstyles)((system.windows.forms.anchorstyles.bottom | system.windows.forms.anchorstyles.left)));
this.button1.location = new system.drawing.point(8, 312);
this.button1.name = "button1";
this.button1.size = new system.drawing.size(56, 23);
this.button1.tabindex = 1;
this.button1.text = "loadxml";
this.button1.click += new system.eventhandler(this.button1_click);
//
// button2
//
this.button2.anchor = ((system.windows.forms.anchorstyles)((system.windows.forms.anchorstyles.bottom | system.windows.forms.anchorstyles.left)));
this.button2.location = new system.drawing.point(72, 312);
this.button2.name = "button2";
this.button2.size = new system.drawing.size(96, 23);
this.button2.tabindex = 2;
this.button2.text = "loadxmlreader";
this.button2.click += new system.eventhandler(this.button2_click);
//
// button3
//
this.button3.anchor = ((system.windows.forms.anchorstyles)((system.windows.forms.anchorstyles.bottom | system.windows.forms.anchorstyles.left)));
this.button3.location = new system.drawing.point(176, 312);
this.button3.name = "button3";
this.button3.size = new system.drawing.size(56, 23);
this.button3.tabindex = 3;
this.button3.text = "savexml";
this.button3.click += new system.eventhandler(this.button3_click);
//
// button4
//
this.button4.anchor = ((system.windows.forms.anchorstyles)((system.windows.forms.anchorstyles.bottom | system.windows.forms.anchorstyles.left)));
this.button4.location = new system.drawing.point(240, 312);
this.button4.name = "button4";
this.button4.size = new system.drawing.size(96, 23);
this.button4.tabindex = 4;
this.button4.text = "savexmlwriter";
this.button4.click += new system.eventhandler(this.button4_click);
//
// button5
//
this.button5.anchor = ((system.windows.forms.anchorstyles)((system.windows.forms.anchorstyles.bottom | system.windows.forms.anchorstyles.left)));
this.button5.location = new system.drawing.point(344, 312);
this.button5.name = "button5";
this.button5.size = new system.drawing.size(80, 23);
this.button5.tabindex = 5;
this.button5.text = "add product";
this.button5.click += new system.eventhandler(this.button5_click);
//
// button6
//
this.button6.anchor = ((system.windows.forms.anchorstyles)((system.windows.forms.anchorstyles.bottom | system.windows.forms.anchorstyles.left)));
this.button6.location = new system.drawing.point(432, 312);
this.button6.name = "button6";
this.button6.size = new system.drawing.size(112, 23);
this.button6.tabindex = 6;
this.button6.text = "replace product";
this.button6.click += new system.eventhandler(this.button6_click);
//
// button7
//
this.button7.anchor = ((system.windows.forms.anchorstyles)((system.windows.forms.anchorstyles.bottom | system.windows.forms.anchorstyles.left)));
this.button7.location = new system.drawing.point(552, 312);
this.button7.name = "button7";
this.button7.size = new system.drawing.size(88, 23);
this.button7.tabindex = 7;
this.button7.text = "change order";
this.button7.click += new system.eventhandler(this.button7_click);
//
// button8
//
this.button8.anchor = ((system.windows.forms.anchorstyles)((system.windows.forms.anchorstyles.bottom | system.windows.forms.anchorstyles.left)));
this.button8.location = new system.drawing.point(648, 312);
this.button8.name = "button8";
this.button8.size = new system.drawing.size(136, 23);
this.button8.tabindex = 8;
this.button8.text = "remove product info";
this.button8.click += new system.eventhandler(this.button8_click);
//
// form1
//
this.autoscalebasesize = new system.drawing.size(6, 14);
this.clientsize = new system.drawing.size(792, 341);
this.controls.add(this.button8);
this.controls.add(this.button7);
this.controls.add(this.button6);
this.controls.add(this.button5);
this.controls.add(this.button4);
this.controls.add(this.button3);
this.controls.add(this.button2);
this.controls.add(this.button1);
this.controls.add(this.textbox1);
this.name = "form1";
this.text = "domsample application";
this.resumelayout(false);
//
// xmlpath
//
this.xmlpath = "../../sample.xml";
}
#endregion
/// <summary>
/// 應用程序的主入口點。
/// </summary>
[stathread]
static void main()
{
application.run(new form1());
}
private void button1_click(object sender, system.eventargs e)
{
this.textbox1.text = "";
domoperation domoper = new domoperation(this.xmlpath);
try
{
this.textbox1.text = domoper.load();
}
catch(xmlexception xmlexp)
{
messagebox.show(xmlexp.tostring(),"error",messageboxbuttons.ok,messageboxicon.error);
}
catch(exception exp)
{
messagebox.show(exp.tostring(),"error",messageboxbuttons.ok,messageboxicon.error);
}
}
private void button2_click(object sender, system.eventargs e)
{
this.textbox1.text = "";
domoperation domoper = new domoperation(this.xmlpath);
try
{
this.textbox1.text = domoper.loadbyxmlreader(xmlnodetype.element,"product");
}
catch(xmlexception xmlexp)
{
messagebox.show(xmlexp.tostring(),"error",messageboxbuttons.ok,messageboxicon.error);
}
catch(exception exp)
{
messagebox.show(exp.tostring(),"error",messageboxbuttons.ok,messageboxicon.error);
}
}
private void button3_click(object sender, system.eventargs e)
{
string savepath = "../../rootdoc.xml";
string xmltext = "<root><para>some para text</para></root>";
domoperation domoper = new domoperation();
try
{
this.textbox1.text = domoper.save(xmltext,savepath);
}
catch(xmlexception xmlexp)
{
messagebox.show(xmlexp.tostring(),"error",messageboxbuttons.ok,messageboxicon.error);
}
catch(exception exp)
{
messagebox.show(exp.tostring(),"error",messageboxbuttons.ok,messageboxicon.error);
}
}
private void button4_click(object sender, system.eventargs e)
{
string savepath = "../../rootdoc2.xml";
string xmltext = "<?xml version='1.0' encoding='utf-8' ?><root><para>some para text</para></root>";
domoperation domoper = new domoperation();
try
{
this.textbox1.text = domoper.savebyxmlwriter(xmltext,savepath);
}
catch(xmlexception xmlexp)
{
messagebox.show(xmlexp.tostring(),"error",messageboxbuttons.ok,messageboxicon.error);
}
catch(exception exp)
{
messagebox.show(exp.tostring(),"error",messageboxbuttons.ok,messageboxicon.error);
}
}
private void button5_click(object sender, system.eventargs e)
{
domoperation domoper = new domoperation();
xmldocument xmldoc = new xmldocument();
try
{
xmldoc.load(this.xmlpath);
// 創建一個xml文檔片段
xmldocumentfragment xmldocfrag = xmldoc.createdocumentfragment();
// 創建父根元素節點test:product
xmlelement proele = xmldoc.createelement("test","product","uri:test");
// 創建屬性并添加到根元素test:product中
xmlattribute proidatt = xmldoc.createattribute("productid");
proidatt.value = "mu78";
proele.attributes.setnameditem(proidatt);
// 創建根元素節點的葉節點
xmlelement colorele = xmldoc.createelement("color");
colorele.innertext = "red";
proele.appendchild(colorele);
xmlelement sizeele = xmldoc.createelement("size");
sizeele.innertext = "m";
proele.appendchild(sizeele);
xmlelement priceele = xmldoc.createelement("price");
priceele.innertext = "125.99";
proele.appendchild(priceele);
// 將根元素添加進xml文檔片段中
xmldocfrag.appendchild(proele);
this.textbox1.text = domoper.addchildnode(xmldoc,xmldocfrag,"//productfamily");
}
catch(exception exp)
{
messagebox.show(exp.tostring(),"error",messageboxbuttons.ok,messageboxicon.error);
}
}
private void button6_click(object sender, system.eventargs e)
{
xmldocument xmldoc = new xmldocument();
domoperation domoper = new domoperation();
try
{
xmldoc.load("../../sample.xml");
xmldocumentfragment xmldocfrag = xmldoc.createdocumentfragment();
xmlelement proele = xmldoc.createelement("test","product","uri:test");
xmlattribute proidatt = xmldoc.createattribute("productid");
proidatt.value = "mu78";
proele.attributes.setnameditem(proidatt);
xmlelement colorele = xmldoc.createelement("color");
colorele.innertext = "red";
proele.appendchild(colorele);
xmlelement sizeele = xmldoc.createelement("size");
sizeele.innertext = "m";
proele.appendchild(sizeele);
xmlelement priceele = xmldoc.createelement("price");
priceele.innertext = "125.99";
proele.appendchild(priceele);
xmldocfrag.appendchild(proele);
this.textbox1.text = domoper.replacechildnode(xmldoc,xmldocfrag,"//productfamily",0);
}
catch(exception exp)
{
messagebox.show(exp.tostring(),"error",messageboxbuttons.ok,messageboxicon.error);
}
}
// 使用innerxml和innertext直接修改xml文檔
private void button7_click(object sender, system.eventargs e)
{
xmldocument xmldoc = new xmldocument();
xmldoc.load("../../sample.xml");
xmlnodelist nodelist = xmldoc.selectnodes("//price");
foreach (xmlnode node in nodelist)
{
node.innerxml = "<currency type='dollar'>" + node.innertext + "</currency>";
}
this.textbox1.text = xmldoc.outerxml;
}
private void button8_click(object sender, system.eventargs e)
{
domoperation domoper = new domoperation(this.xmlpath);
try
{
this.textbox1.text = domoper.removechildnode("company","productfamily");
}
catch(exception exp)
{
messagebox.show(exp.tostring(),"error",messageboxbuttons.ok,messageboxicon.error);
}
}
}
}
程序中使用到的xml文件如下:
sample.xml
<?xml version="1.0" encoding="utf-8" ?>
<catalog xmlns:test="uri:test">
<company email="[email protected]" name="celtic productions">
<productfamily familyid="pftops" lastupdate="2001-08-26t 18:39:09" buyersuri="http://www.deltabis.com/buyers/tops">
<test:product productid="cfc4">
<color>black</color>
<size>l</size>
<price>32.99</price>
</test:product>
</productfamily>
</company>
</catalog>