首页 > 开发 > Xml > 正文

C#中使用XML——实现DOM

2020-02-03 14:11:21
字体:
来源:转载
供稿:网友
菜鸟学堂:
在前两篇文章中我们讨论了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>


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表