C# XML DOM 实现

None.gif LoadXML按纽用于加载XML文档,LoadXMLReader按纽使用XmlTextReader加载文档,SaveXML按纽保存文档,SaveXMLWriter按纽将文档保存到XmlTextWriter中,Add Product按纽添加节点,Replace Product按纽替换节点,Change Order按纽修改文档,Remove Product Info按纽移除节点。 
None.gif
None.gif
None.gif
None.gifDomOperation类封装了所有按纽功能的实现,代码如下: 
None.gif
None.gif DOM的全称是Document Object Model(文档对象模型),它是来自W3C的官方标准,它允许按照W3C标准W3C DOM Level1和W3C DOM Level2的规范所定义的规则,通过编程来读取,操纵和修改XML文档。DOM的工作方式是:首先将XML文档一次性的装入内存,然后根据文档中定义的元素和属性在内存中创建一个“树型结构”也就是一个文档对象模型,这里的含义其实是把文档对象化,文档中每个节点对应着模型中一个对象,而我们都知道对象提供编程接口,所以在Application中我们正是使用这组对象来访问XML文档进而操作XML文档,下图阐述了Application和DOM交互的过程: 
None.gif
None.gif
None.gif
None.gif
None.gifDOM既然是在内存中创建树型结构视图进而提供编程接口,那我们就以下面这个XML片段来说明DOM是如何创建树型结构的: 
None.gif
None.gif
< parent >  
None.gif
None.gif
< child  id =”123” > text here </ child >  
None.gif
None.gif
</ parent >  
None.gif
None.gif如果用DOM加载以上文档,它将在内存中创建的树型结构如下图: 
None.gif
None.gif
None.gif
None.gif
None.gifDOM的关键在于它允许直接更新内存中的树型结构,而不必重定向到其他输出,因此,添加、更新或删除结构中信息的操作效率更高。而作为程序员的我们重要的是要了解DOM所提供的编程接口以实现对XML文档进行操作,事实上,.NET Framework定义了一组类用于反映DOM的体系结构,下面来看一下.NET DOM的继承结构: 
None.gif
None.gif
None.gif
None.gif
None.gif在上图中所有弧角矩形中所包含的类描述了所有可能在XML文档中出现的节点类型,而操作XML文档不外乎是操作其中的节点,这些类又都是从XmlNode类派生而来,所以我们今天的主题是讨论XmlNode类和它的子类XmlDocument,下面对这些类做简单的介绍: 
None.gif
None.gifXmlNode类: 
None.gif
None.gif该类是DOM中所有其他节点的抽象基类,它定义所有在更低级的类中继承或重写的成员。它表示XML文档中的单一节点,它提供了用于导航DOM树型结构的基本方法和属性,使用XmlNodeType枚举器可以枚举其下的所有节点类型。以下介绍该类的部分属性和方法: 
None.gif
None.gif属性: 
None.gif
None.gif[C#] 
None.gif
None.gifpublic virtual bool HasChildNodes {get;} 获取一个值,该值指示当前节点是否有任何子节点 
None.gif
None.gifpublic virtual XmlNodeList ChildNodes {get;} 获取当前节点的所有子节点 
None.gif
None.gifpublic virtual XmlNode FirstChild {get;} 获取当前节点的第一个子级 
None.gif
None.gifpublic virtual XmlNode LastChild {get;} 获取当前节点的最后一个子级 
None.gif
None.gifpublic virtual XmlNode ParentNode {get;} 获取当前节点的父级 
None.gif
None.gifpublic virtual XmlNode NextSibling {get;} 获取当前节点的下一个兄弟节点 
None.gif
None.gifpublic virtual XmlNode PreviousSibling {get;} 获取当前节点的上一个兄弟节点 
None.gif
None.gifpublic virtual string InnerText {get; set;} 获取或设置当前节点及其所有子节点的文本内容的串联值 
None.gif
None.gifpublic virtual string InnerXml {get; set;} 获取或设置仅代表当前节点的子节点的标记 
None.gif
None.gifpublic virtual string OuterXml {get;} 获取表示当前节点及其所有子节点的标记 
None.gif
None.gif方法: 
None.gif
None.gifpublic XmlNodeList SelectNodes(string); 选择文档中匹配 XPath 表达式的节点列表 
None.gif
None.gifpublic XmlNode SelectSingleNode(string); 选择文档中匹配 XPath 表达式的第一个 XmlNode 
None.gif
None.gifpublic virtual XmlNode AppendChild(XmlNode newChild) 将指定的节点添加到该节点的子节点列表的末尾 
None.gif
None.gifpublic virtual XmlNode PrependChild(XmlNode newChild) 将指定的节点添加到该节点的子节点列表的开头 
None.gif
None.gifpublic virtual XmlNode RemoveChild(XmlNode oldChild) 移除指定的子节点 
None.gif
None.gifpublic virtual XmlNode ReplaceChild(XmlNode newChild,XmlNode oldChild) 用 newChild 节点替换子节点 oldChild 
None.gif
None.gifXmlNodeList类: 
None.gif
None.gif该类表示XmlNode的有序集合,它有以下常用成员: 
None.gif
None.gifCount——以整数形式返回XmlNodeList中的节点数 
None.gif
None.gifItemOf——搜索在指定索引处的节点 
None.gif
None.gifGetEnumerator()——提供迭代遍历节点列表的foreach样式 
None.gif
None.gifItem()——返回参数指定的索引处的节点 
None.gif
None.gifXmlDocument类: 
None.gif
None.gifXmlDocument类是XML文档的.NET表示形式,它代表了内存中树型结构的文档节点(所有的节点都在文档节点下),XmlDocument类包含所有的CreateXXX()方法,这些方法允许创建所有派生自XmlNode的类型的节点,通常将该类与XmlNode类一起使用以完成对文档的操作,该类有一个Load()方法用于加载XML文档,该方法的一个重载版本允许从XmlTextReader加载文档,这给我们带来的好处是在操作较大的文档时我们可以先使用XmlTextReader过滤不相关的文档部分,这样即解决了DOM所带来的资源损耗问题又可以保留DOM对文档操控的便利性,该类的Save()方法用于保存文档。 
None.gif
None.gif namespace  DOMSamples 
None.gif
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
InBlock.gif
InBlock.gif
using System; 
InBlock.gif
InBlock.gif
using System.Xml; 
InBlock.gif
InBlock.gif
using System.Text; 
InBlock.gif
InBlock.gif
using System.Windows.Forms; 
InBlock.gif
InBlock.gif
using System.ComponentModel; 
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// <summary> 
InBlock.gif
InBlock.gif
/// DomOperation 提供对Xml文件操作的类 
InBlock.gif
InBlock.gif
/// </summary> 
InBlock.gif
InBlock.gif
/// <remarks> 
InBlock.gif
InBlock.gif
/// 该类用于提供对XML文件进行一般的操作,如(添加,删除,替换节点,加载,保存文件)等,该类内部实现采用DOM技术。 
InBlock.gif
ExpandedSubBlockEnd.gif
/// </remarks> 

InBlock.gif
InBlock.gif
public class DomOperation : IDisposable 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
InBlock.gif
private string _xmlPath; 
InBlock.gif
InBlock.gif
private XmlDocument xmlDoc; 
InBlock.gif
InBlock.gif
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif
DomOperation 构造器#region DomOperation 构造器 
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// <summary> 
InBlock.gif
InBlock.gif
/// 默认构造器,对该类成员进行默认赋值 
InBlock.gif
ExpandedSubBlockEnd.gif
/// </summary> 

InBlock.gif
InBlock.gif
public DomOperation() 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
InBlock.gif
this._xmlPath = string.Empty; 
InBlock.gif
InBlock.gif
this.xmlDoc = null
InBlock.gif
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// <summary> 
InBlock.gif
InBlock.gif
/// 带参构造器,对该类成员进行初始赋值 
InBlock.gif
InBlock.gif
/// </summary> 
InBlock.gif
ExpandedSubBlockEnd.gif
/// <param name="xmlPath">xml文件路径</param> 

InBlock.gif
InBlock.gif
public DomOperation(string xmlPath) 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
InBlock.gif
this._xmlPath = xmlPath; 
InBlock.gif
InBlock.gif
this.xmlDoc = null
InBlock.gif
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif
#endregion
 
InBlock.gif
InBlock.gif
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif
DomOperation 资源释放方法#region DomOperation 资源释放方法 
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// <summary> 
InBlock.gif
InBlock.gif
/// 清理该对象所有正在使用的资源 
InBlock.gif
ExpandedSubBlockEnd.gif
/// </summary> 

InBlock.gif
InBlock.gif
public void Dispose() 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
InBlock.gif
this.Dispose(true); 
InBlock.gif
InBlock.gifGC.SuppressFinalize(
this); 
InBlock.gif
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// <summary> 
InBlock.gif
InBlock.gif
/// 释放该对象的实例变量 
InBlock.gif
InBlock.gif
/// </summary> 
InBlock.gif
ExpandedSubBlockEnd.gif
/// <param name="disposing"></param> 

InBlock.gif
InBlock.gif
protected virtual void Dispose(bool disposing) 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
InBlock.gif
if (!disposing) 
InBlock.gif
InBlock.gif
return
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
if (this._xmlPath != null
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
InBlock.gif
this._xmlPath = null
InBlock.gif
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
if (this.xmlDoc != null
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
InBlock.gif
this.xmlDoc = null
InBlock.gif
ExpandedSubBlockEnd.gif}
 
InBlock.gif
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif
#endregion
 
InBlock.gif
InBlock.gif
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif
DomOperation 属性#region DomOperation 属性 
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// <summary> 
InBlock.gif
InBlock.gif
/// 获取或设置Xml文件的路径 
InBlock.gif
ExpandedSubBlockEnd.gif
/// </summary> 

InBlock.gif
InBlock.gif
public string xmlPath 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
InBlock.gif
get 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
InBlock.gif
return _xmlPath; 
InBlock.gif
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gif
set 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
InBlock.gif
this._xmlPath = value; 
InBlock.gif
ExpandedSubBlockEnd.gif}
 
InBlock.gif
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif
#endregion
 
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// <summary> 
InBlock.gif
InBlock.gif
/// 加载XML文件 
InBlock.gif
InBlock.gif
/// </summary> 
InBlock.gif
InBlock.gif
/// <remarks> 
InBlock.gif
InBlock.gif
/// 该方法使用XML文件的路径加载XML文件并返回整个XML文件的内容 
InBlock.gif
InBlock.gif
/// </remarks> 
InBlock.gif
ExpandedSubBlockEnd.gif
/// <returns>整个XML文件的内容</returns> 

InBlock.gif
InBlock.gif
public string Load() 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
InBlock.gifxmlDoc 
= new XmlDocument(); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
try 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
InBlock.gifxmlDoc.Load(
this._xmlPath); 
InBlock.gif
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gif
catch(XmlException xmlExp) 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
InBlock.gif
throw new XmlException(xmlExp.ToString()); 
InBlock.gif
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
return xmlDoc.OuterXml; 
InBlock.gif
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// <summary> 
InBlock.gif
InBlock.gif
/// 加载XML文件 
InBlock.gif
InBlock.gif
/// </summary> 
InBlock.gif
InBlock.gif
/// <remarks> 
InBlock.gif
InBlock.gif
/// 该方法使用XmlReader在XML文档中定位并加载XML文件最后返回XML文件的内容 
InBlock.gif
InBlock.gif
/// </remarks> 
InBlock.gif
InBlock.gif
/// <param name="nodeType">将要定位的节点类型</param> 
InBlock.gif
InBlock.gif
/// <param name="localName">将要定位的节点名称</param> 
InBlock.gif
ExpandedSubBlockEnd.gif
/// <returns>XML文件的内容</returns> 

InBlock.gif
InBlock.gif
public string LoadByXmlReader(XmlNodeType nodeType, string localName) 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
InBlock.gif
string xmlStr = string.Empty; 
InBlock.gif
InBlock.gifXmlTextReader xmlTxtRd 
= new XmlTextReader(this._xmlPath); 
InBlock.gif
InBlock.gifxmlDoc 
= new XmlDocument(); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
try 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
InBlock.gif
// 在文档中定位 
InBlock.gif

InBlock.gif
while(xmlTxtRd.Read()) 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
InBlock.gif
if (xmlTxtRd.NodeType == nodeType) 
InBlock.gif
InBlock.gif
if(xmlTxtRd.LocalName == localName) 
InBlock.gif
InBlock.gif
break
InBlock.gif
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gifxmlDoc.Load(xmlTxtRd); 
InBlock.gif
InBlock.gifxmlTxtRd.Close(); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gifxmlStr 
+= "===== OuterXml ====="
InBlock.gif
InBlock.gifxmlStr 
+= System.Environment.NewLine; 
InBlock.gif
InBlock.gifxmlStr 
+= xmlDoc.FirstChild.OuterXml; 
InBlock.gif
InBlock.gifxmlStr 
+= System.Environment.NewLine; 
InBlock.gif
InBlock.gifxmlStr 
+= System.Environment.NewLine; 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gifxmlStr 
+= "===== InnerXml ====="
InBlock.gif
InBlock.gifxmlStr 
+= System.Environment.NewLine; 
InBlock.gif
InBlock.gifxmlStr 
+= xmlDoc.FirstChild.InnerXml; 
InBlock.gif
InBlock.gifxmlStr 
+= System.Environment.NewLine; 
InBlock.gif
InBlock.gifxmlStr 
+= System.Environment.NewLine; 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gifxmlStr 
+= "===== InnerText ====="
InBlock.gif
InBlock.gifxmlStr 
+= System.Environment.NewLine; 
InBlock.gif
InBlock.gifxmlStr 
+= xmlDoc.FirstChild.InnerText; 
InBlock.gif
InBlock.gifxmlStr 
+= System.Environment.NewLine; 
InBlock.gif
InBlock.gifxmlStr 
+= System.Environment.NewLine; 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gifxmlStr 
+= "===== Value ====="
InBlock.gif
InBlock.gifxmlStr 
+= System.Environment.NewLine; 
InBlock.gif
InBlock.gifxmlStr 
+= xmlDoc.FirstChild.ChildNodes[0].ChildNodes[0].Value + " " + 
InBlock.gif
InBlock.gifxmlDoc.FirstChild.ChildNodes[
1].ChildNodes[0].Value + " " + 
InBlock.gif
InBlock.gifxmlDoc.FirstChild.ChildNodes[
2].ChildNodes[0].Value; 
InBlock.gif
InBlock.gifxmlStr 
+= System.Environment.NewLine; 
InBlock.gif
InBlock.gifxmlStr 
+= System.Environment.NewLine; 
InBlock.gif
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gif
catch(XmlException xmlExp) 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
InBlock.gif
throw new XmlException(xmlExp.ToString()); 
InBlock.gif
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gif
finally 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
InBlock.gif
if (xmlTxtRd != null && xmlTxtRd.ReadState != ReadState.Closed) 
InBlock.gif
InBlock.gifxmlTxtRd.Close(); 
InBlock.gif
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
return xmlStr; 
InBlock.gif
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// <summary> 
InBlock.gif
InBlock.gif
/// 保存XML文件 
InBlock.gif
InBlock.gif
/// </summary> 
InBlock.gif
InBlock.gif
/// <remarks> 
InBlock.gif
InBlock.gif
/// 该方法将传入的XML文本保存在传入的路径中最后返回XML文件的内容 
InBlock.gif
InBlock.gif
/// </remarks> 
InBlock.gif
InBlock.gif
/// <param name="xmlText">XML文本</param> 
InBlock.gif
InBlock.gif
/// <param name="savePath">保存路径</param> 
InBlock.gif
ExpandedSubBlockEnd.gif
/// <returns>XML文件的内容</returns> 

InBlock.gif
InBlock.gif
public string Save(string xmlText, string savePath) 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
InBlock.gifxmlDoc 
= new XmlDocument(); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
try 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
InBlock.gifxmlDoc.LoadXml(xmlText); 
InBlock.gif
InBlock.gifxmlDoc.Save(savePath); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gifxmlDoc.Load(savePath); 
InBlock.gif
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gif
catch(XmlException xmlExp) 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
InBlock.gif
throw new XmlException(xmlExp.ToString()); 
InBlock.gif
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
return xmlDoc.OuterXml; 
InBlock.gif
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// <summary> 
InBlock.gif
InBlock.gif
/// 保存XML文件 
InBlock.gif
InBlock.gif
/// </summary> 
InBlock.gif
InBlock.gif
/// <remarks> 
InBlock.gif
InBlock.gif
/// 该方法将传入的XML文本保存在XmlTextWriter编写器中并使用传入的路径构造该编写器最后返回XML文件的内容 
InBlock.gif
InBlock.gif
/// </remarks> 
InBlock.gif
InBlock.gif
/// <param name="xmlText">XML文本</param> 
InBlock.gif
InBlock.gif
/// <param name="savePath">保存路径</param> 
InBlock.gif
ExpandedSubBlockEnd.gif
/// <returns>XML文件的内容</returns> 

InBlock.gif
InBlock.gif
public string SaveByXmlWriter(string xmlText, string savePath) 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
InBlock.gifXmlTextWriter xmlTxtWt 
= new XmlTextWriter(savePath,Encoding.Unicode); 
InBlock.gif
InBlock.gifxmlDoc 
= new XmlDocument(); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
try 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
InBlock.gifxmlDoc.LoadXml(xmlText); 
InBlock.gif
InBlock.gifxmlDoc.Save(xmlTxtWt); 
InBlock.gif
InBlock.gifxmlTxtWt.Close(); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gifxmlDoc.Load(savePath); 
InBlock.gif
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gif
catch(XmlException xmlExp) 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
InBlock.gif
throw new XmlException(xmlExp.ToString()); 
InBlock.gif
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gif
finally 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
InBlock.gif
if (xmlTxtWt != null && xmlTxtWt.WriteState != WriteState.Closed) 
InBlock.gif
InBlock.gifxmlTxtWt.Close(); 
InBlock.gif
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
return xmlDoc.OuterXml; 
InBlock.gif
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// <summary> 
InBlock.gif
InBlock.gif
/// 添加子节点 
InBlock.gif
InBlock.gif
/// </summary> 
InBlock.gif
InBlock.gif
/// <remarks> 
InBlock.gif
InBlock.gif
/// 该方法利用传入的父节点路径选择该节点并将传入的XML文档片段添加到该父节点下最后返回添加后XML文件的内容 
InBlock.gif
InBlock.gif
/// </remarks> 
InBlock.gif
InBlock.gif
/// <param name="xmlDoc"></param> 
InBlock.gif
InBlock.gif
/// <param name="xmlDocFrag">XML文档片段</param> 
InBlock.gif
InBlock.gif
/// <param name="parentNodePath">父节点路径</param> 
InBlock.gif
ExpandedSubBlockEnd.gif
/// <returns>添加后XML文件的内容</returns> 

InBlock.gif
InBlock.gif
public string AddChildNode(XmlDocument xmlDoc, XmlDocumentFragment xmlDocFrag, string parentNodePath) 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
InBlock.gif
this.xmlDoc = xmlDoc; 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gifXmlElement selectEle 
= (XmlElement)xmlDoc.SelectSingleNode(parentNodePath); 
InBlock.gif
InBlock.gifselectEle.AppendChild(xmlDocFrag.FirstChild); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
return this.xmlDoc.OuterXml; 
InBlock.gif
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// <summary> 
InBlock.gif
InBlock.gif
/// 替换子节点 
InBlock.gif
InBlock.gif
/// </summary> 
InBlock.gif
InBlock.gif
/// <remarks> 
InBlock.gif
InBlock.gif
/// 该方法利用父节点路径选择该节点并用传入的XML文档片段替换该父节点下的第i个子节点最后返回替换后的XML文件的内容 
InBlock.gif
InBlock.gif
/// </remarks> 
InBlock.gif
InBlock.gif
/// <param name="xmlDoc"></param> 
InBlock.gif
InBlock.gif
/// <param name="xmlDocFrag">XML文档片段</param> 
InBlock.gif
InBlock.gif
/// <param name="parentNodePath">父节点路径</param> 
InBlock.gif
InBlock.gif
/// <param name="i">第i个子节点</param> 
InBlock.gif
ExpandedSubBlockEnd.gif
/// <returns>替换后的XML文件的内容</returns> 

InBlock.gif
InBlock.gif
public string ReplaceChildNode(XmlDocument xmlDoc, XmlDocumentFragment xmlDocFrag, string parentNodePath, int i) 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
InBlock.gif
this.xmlDoc = xmlDoc; 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gifXmlElement selectEle 
= (XmlElement)xmlDoc.SelectSingleNode(parentNodePath); 
InBlock.gif
InBlock.gifXmlElement childEle 
= (XmlElement)selectEle.ChildNodes[i]; 
InBlock.gif
InBlock.gifselectEle.ReplaceChild(xmlDocFrag.FirstChild,childEle); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
return this.xmlDoc.OuterXml; 
InBlock.gif
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// <summary> 
InBlock.gif
InBlock.gif
/// 移除子节点 
InBlock.gif
InBlock.gif
/// </summary> 
InBlock.gif
InBlock.gif
/// <remarks> 
InBlock.gif
InBlock.gif
/// 该方法利用传入的父节点名称选择该父节点并利用传入的子节点名称选择该子节点选定后使用父节点的RemoveChild方法移除子节点 
InBlock.gif
InBlock.gif
/// 最后返回移除后XML的文件内容 
InBlock.gif
InBlock.gif
/// </remarks> 
InBlock.gif
InBlock.gif
/// <param name="parentNodeName">父节点名称</param> 
InBlock.gif
InBlock.gif
/// <param name="childNodeName">子节点名称</param> 
InBlock.gif
ExpandedSubBlockEnd.gif
/// <returns>移除后XML的文件内容</returns> 

InBlock.gif
InBlock.gif
public string RemoveChildNode(string parentNodeName, string childNodeName) 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
InBlock.gifxmlDoc 
= new XmlDocument(); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gifxmlDoc.Load(
this._xmlPath); 
InBlock.gif
InBlock.gifXmlNodeList parentNodeList 
= xmlDoc.GetElementsByTagName(parentNodeName); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
foreach (XmlNode parentNode in parentNodeList) 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
InBlock.gifXmlNodeList childNodeList 
= parentNode.SelectNodes(childNodeName); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
foreach (XmlNode childNode in childNodeList) 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
InBlock.gifparentNode.RemoveChild(childNode); 
InBlock.gif
ExpandedSubBlockEnd.gif}
 
InBlock.gif
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
return xmlDoc.OuterXml; 
InBlock.gif
ExpandedSubBlockEnd.gif}
 
InBlock.gif
ExpandedSubBlockEnd.gif}
 
InBlock.gif
ExpandedBlockEnd.gif}
 
None.gif
None.gif窗口程序代码如下: 
None.gif
None.gif
namespace  DOMSamples 
None.gif
ExpandedBlockStart.gifContractedBlock.gif
dot.gif
InBlock.gif
InBlock.gif
using System; 
InBlock.gif
InBlock.gif
using System.Xml; 
InBlock.gif
InBlock.gif
using System.Text; 
InBlock.gif
InBlock.gif
using System.Drawing; 
InBlock.gif
InBlock.gif
using System.Collections; 
InBlock.gif
InBlock.gif
using System.ComponentModel; 
InBlock.gif
InBlock.gif
using System.Windows.Forms; 
InBlock.gif
InBlock.gif
using System.Data; 
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// <summary> 
InBlock.gif
InBlock.gif
/// Form1 的摘要说明。 
InBlock.gif
ExpandedSubBlockEnd.gif
/// </summary> 

InBlock.gif
InBlock.gif
public class Form1 : System.Windows.Forms.Form 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
InBlock.gif
private System.Windows.Forms.TextBox textBox1; 
InBlock.gif
InBlock.gif
private System.Windows.Forms.Button button1; 
InBlock.gif
InBlock.gif
private System.Windows.Forms.Button button2; 
InBlock.gif
InBlock.gif
private System.Windows.Forms.Button button3; 
InBlock.gif
InBlock.gif
private System.Windows.Forms.Button button4; 
InBlock.gif
InBlock.gif
private System.Windows.Forms.Button button5; 
InBlock.gif
InBlock.gif
private System.Windows.Forms.Button button6; 
InBlock.gif
InBlock.gif
private System.Windows.Forms.Button button7; 
InBlock.gif
InBlock.gif
private System.Windows.Forms.Button button8; 
InBlock.gif
InBlock.gif
private string xmlPath; 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// <summary> 
InBlock.gif
InBlock.gif
/// 必需的设计器变量。 
InBlock.gif
ExpandedSubBlockEnd.gif
/// </summary> 

InBlock.gif
InBlock.gif
private System.ComponentModel.Container components = null
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
public Form1() 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
InBlock.gif
// 
InBlock.gif
InBlock.gif
// Windows 窗体设计器支持所必需的 
InBlock.gif
InBlock.gif
// 
InBlock.gif

InBlock.gifInitializeComponent(); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
// 
InBlock.gif
InBlock.gif
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码 
InBlock.gif
InBlock.gif
// 
InBlock.gif

ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// <summary> 
InBlock.gif
InBlock.gif
/// 清理所有正在使用的资源。 
InBlock.gif
ExpandedSubBlockEnd.gif
/// </summary> 

InBlock.gif
InBlock.gif
protected override void Dispose( bool disposing ) 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
InBlock.gif
if( disposing ) 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
InBlock.gif
if (components != null
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
InBlock.gifcomponents.Dispose(); 
InBlock.gif
ExpandedSubBlockEnd.gif}
 
InBlock.gif
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gif
base.Dispose( disposing ); 
InBlock.gif
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gif
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif
Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// <summary> 
InBlock.gif
InBlock.gif
/// 设计器支持所需的方法 - 不要使用代码编辑器修改 
InBlock.gif
InBlock.gif
/// 此方法的内容。 
InBlock.gif
ExpandedSubBlockEnd.gif
/// </summary> 

InBlock.gif
InBlock.gif
private void InitializeComponent() 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
InBlock.gif
this.textBox1 = new System.Windows.Forms.TextBox(); 
InBlock.gif
InBlock.gif
this.button1 = new System.Windows.Forms.Button(); 
InBlock.gif
InBlock.gif
this.button2 = new System.Windows.Forms.Button(); 
InBlock.gif
InBlock.gif
this.button3 = new System.Windows.Forms.Button(); 
InBlock.gif
InBlock.gif
this.button4 = new System.Windows.Forms.Button(); 
InBlock.gif
InBlock.gif
this.button5 = new System.Windows.Forms.Button(); 
InBlock.gif
InBlock.gif
this.button6 = new System.Windows.Forms.Button(); 
InBlock.gif
InBlock.gif
this.button7 = new System.Windows.Forms.Button(); 
InBlock.gif
InBlock.gif
this.button8 = new System.Windows.Forms.Button(); 
InBlock.gif
InBlock.gif
this.SuspendLayout(); 
InBlock.gif
InBlock.gif
// 
InBlock.gif
InBlock.gif
// textBox1 
InBlock.gif
InBlock.gif
// 
InBlock.gif

InBlock.gif
this.textBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
InBlock.gif
InBlock.gif
| System.Windows.Forms.AnchorStyles.Left) 
InBlock.gif
InBlock.gif
| System.Windows.Forms.AnchorStyles.Right))); 
InBlock.gif
InBlock.gif
this.textBox1.Location = new System.Drawing.Point(88); 
InBlock.gif
InBlock.gif
this.textBox1.Multiline = true
InBlock.gif
InBlock.gif
this.textBox1.Name = "textBox1"
InBlock.gif
InBlock.gif
this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Both; 
InBlock.gif
InBlock.gif
this.textBox1.Size = new System.Drawing.Size(776296); 
InBlock.gif
InBlock.gif
this.textBox1.TabIndex = 0
InBlock.gif
InBlock.gif
this.textBox1.Text = ""
InBlock.gif
InBlock.gif
// 
InBlock.gif
InBlock.gif
// button1 
InBlock.gif
InBlock.gif
// 
InBlock.gif

InBlock.gif
this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); 
InBlock.gif
InBlock.gif
this.button1.Location = new System.Drawing.Point(8312); 
InBlock.gif
InBlock.gif
this.button1.Name = "button1"
InBlock.gif
InBlock.gif
this.button1.Size = new System.Drawing.Size(5623); 
InBlock.gif
InBlock.gif
this.button1.TabIndex = 1
InBlock.gif
InBlock.gif
this.button1.Text = "LoadXML"
InBlock.gif
InBlock.gif
this.button1.Click += new System.EventHandler(this.button1_Click); 
InBlock.gif
InBlock.gif
// 
InBlock.gif
InBlock.gif
// button2 
InBlock.gif
InBlock.gif
// 
InBlock.gif

InBlock.gif
this.button2.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); 
InBlock.gif
InBlock.gif
this.button2.Location = new System.Drawing.Point(72312); 
InBlock.gif
InBlock.gif
this.button2.Name = "button2"
InBlock.gif
InBlock.gif
this.button2.Size = new System.Drawing.Size(9623); 
InBlock.gif
InBlock.gif
this.button2.TabIndex = 2
InBlock.gif
InBlock.gif
this.button2.Text = "LoadXMLReader"
InBlock.gif
InBlock.gif
this.button2.Click += new System.EventHandler(this.button2_Click); 
InBlock.gif
InBlock.gif
// 
InBlock.gif
InBlock.gif
// button3 
InBlock.gif
InBlock.gif
// 
InBlock.gif

InBlock.gif
this.button3.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); 
InBlock.gif
InBlock.gif
this.button3.Location = new System.Drawing.Point(176312); 
InBlock.gif
InBlock.gif
this.button3.Name = "button3"
InBlock.gif
InBlock.gif
this.button3.Size = new System.Drawing.Size(5623); 
InBlock.gif
InBlock.gif
this.button3.TabIndex = 3
InBlock.gif
InBlock.gif
this.button3.Text = "SaveXML"
InBlock.gif
InBlock.gif
this.button3.Click += new System.EventHandler(this.button3_Click); 
InBlock.gif
InBlock.gif
// 
InBlock.gif
InBlock.gif
// button4 
InBlock.gif
InBlock.gif
// 
InBlock.gif

InBlock.gif
this.button4.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); 
InBlock.gif
InBlock.gif
this.button4.Location = new System.Drawing.Point(240312); 
InBlock.gif
InBlock.gif
this.button4.Name = "button4"
InBlock.gif
InBlock.gif
this.button4.Size = new System.Drawing.Size(9623); 
InBlock.gif
InBlock.gif
this.button4.TabIndex = 4
InBlock.gif
InBlock.gif
this.button4.Text = "SaveXMLWriter"
InBlock.gif
InBlock.gif
this.button4.Click += new System.EventHandler(this.button4_Click); 
InBlock.gif
InBlock.gif
// 
InBlock.gif
InBlock.gif
// button5 
InBlock.gif
InBlock.gif
// 
InBlock.gif

InBlock.gif
this.button5.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); 
InBlock.gif
InBlock.gif
this.button5.Location = new System.Drawing.Point(344312); 
InBlock.gif
InBlock.gif
this.button5.Name = "button5"
InBlock.gif
InBlock.gif
this.button5.Size = new System.Drawing.Size(8023); 
InBlock.gif
InBlock.gif
this.button5.TabIndex = 5
InBlock.gif
InBlock.gif
this.button5.Text = "Add Product"
InBlock.gif
InBlock.gif
this.button5.Click += new System.EventHandler(this.button5_Click); 
InBlock.gif
InBlock.gif
// 
InBlock.gif
InBlock.gif
// button6 
InBlock.gif
InBlock.gif
// 
InBlock.gif

InBlock.gif
this.button6.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); 
InBlock.gif
InBlock.gif
this.button6.Location = new System.Drawing.Point(432312); 
InBlock.gif
InBlock.gif
this.button6.Name = "button6"
InBlock.gif
InBlock.gif
this.button6.Size = new System.Drawing.Size(11223); 
InBlock.gif
InBlock.gif
this.button6.TabIndex = 6
InBlock.gif
InBlock.gif
this.button6.Text = "Replace Product"
InBlock.gif
InBlock.gif
this.button6.Click += new System.EventHandler(this.button6_Click); 
InBlock.gif
InBlock.gif
// 
InBlock.gif
InBlock.gif
// button7 
InBlock.gif
InBlock.gif
// 
InBlock.gif

InBlock.gif
this.button7.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); 
InBlock.gif
InBlock.gif
this.button7.Location = new System.Drawing.Point(552312); 
InBlock.gif
InBlock.gif
this.button7.Name = "button7"
InBlock.gif
InBlock.gif
this.button7.Size = new System.Drawing.Size(8823); 
InBlock.gif
InBlock.gif
this.button7.TabIndex = 7
InBlock.gif
InBlock.gif
this.button7.Text = "Change Order"
InBlock.gif
InBlock.gif
this.button7.Click += new System.EventHandler(this.button7_Click); 
InBlock.gif
InBlock.gif
// 
InBlock.gif
InBlock.gif
// button8 
InBlock.gif
InBlock.gif
// 
InBlock.gif

InBlock.gif
this.button8.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); 
InBlock.gif
InBlock.gif
this.button8.Location = new System.Drawing.Point(648312); 
InBlock.gif
InBlock.gif
this.button8.Name = "button8"
InBlock.gif
InBlock.gif
this.button8.Size = new System.Drawing.Size(13623); 
InBlock.gif
InBlock.gif
this.button8.TabIndex = 8
InBlock.gif
InBlock.gif
this.button8.Text = "Remove Product Info"
InBlock.gif
InBlock.gif
this.button8.Click += new System.EventHandler(this.button8_Click); 
InBlock.gif
InBlock.gif
// 
InBlock.gif
InBlock.gif
// Form1 
InBlock.gif
InBlock.gif
// 
InBlock.gif

InBlock.gif
this.AutoScaleBaseSize = new System.Drawing.Size(614); 
InBlock.gif
InBlock.gif
this.ClientSize = new System.Drawing.Size(792341); 
InBlock.gif
InBlock.gif
this.Controls.Add(this.button8); 
InBlock.gif
InBlock.gif
this.Controls.Add(this.button7); 
InBlock.gif
InBlock.gif
this.Controls.Add(this.button6); 
InBlock.gif
InBlock.gif
this.Controls.Add(this.button5); 
InBlock.gif
InBlock.gif
this.Controls.Add(this.button4); 
InBlock.gif
InBlock.gif
this.Controls.Add(this.button3); 
InBlock.gif
InBlock.gif
this.Controls.Add(this.button2); 
InBlock.gif
InBlock.gif
this.Controls.Add(this.button1); 
InBlock.gif
InBlock.gif
this.Controls.Add(this.textBox1); 
InBlock.gif
InBlock.gif
this.Name = "Form1"
InBlock.gif
InBlock.gif
this.Text = "DOMSample Application"
InBlock.gif
InBlock.gif
this.ResumeLayout(false); 
InBlock.gif
InBlock.gif
// 
InBlock.gif
InBlock.gif
// xmlPath 
InBlock.gif
InBlock.gif
// 
InBlock.gif

InBlock.gif
this.xmlPath = "../../sample.xml"
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif}
 
InBlock.gif
ExpandedSubBlockEnd.gif
#endregion
 
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// <summary> 
InBlock.gif
InBlock.gif
/// 应用程序的主入口点。 
InBlock.gif
ExpandedSubBlockEnd.gif
/// </summary> 

InBlock.gif
InBlock.gif[STAThread] 
InBlock.gif
InBlock.gif
static void Main() 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
InBlock.gifApplication.Run(
new Form1()); 
InBlock.gif
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
private void button1_Click(object sender, System.EventArgs e) 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
InBlock.gif
this.textBox1.Text = ""
InBlock.gif
InBlock.gifDomOperation DomOper 
= new DomOperation(this.xmlPath); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
try 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
InBlock.gif
this.textBox1.Text = DomOper.Load(); 
InBlock.gif
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gif
catch(XmlException xmlExp) 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
InBlock.gifMessageBox.Show(xmlExp.ToString(),
"Error",MessageBoxButtons.OK,MessageBoxIcon.Error); 
InBlock.gif
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gif
catch(Exception exp) 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
InBlock.gifMessageBox.Show(exp.ToString(),
"Error",MessageBoxButtons.OK,MessageBoxIcon.Error); 
InBlock.gif
ExpandedSubBlockEnd.gif}
 
InBlock.gif
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
private void button2_Click(object sender, System.EventArgs e) 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
InBlock.gif
this.textBox1.Text = ""
InBlock.gif
InBlock.gifDomOperation DomOper 
= new DomOperation(this.xmlPath); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
try 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
InBlock.gif
this.textBox1.Text = DomOper.LoadByXmlReader(XmlNodeType.Element,"Product"); 
InBlock.gif
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gif
catch(XmlException xmlExp) 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
InBlock.gifMessageBox.Show(xmlExp.ToString(),
"Error",MessageBoxButtons.OK,MessageBoxIcon.Error); 
InBlock.gif
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gif
catch(Exception exp) 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
InBlock.gifMessageBox.Show(exp.ToString(),
"Error",MessageBoxButtons.OK,MessageBoxIcon.Error); 
InBlock.gif
ExpandedSubBlockEnd.gif}
 
InBlock.gif
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
private void button3_Click(object sender, System.EventArgs e) 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
InBlock.gif
string savePath = "../../rootDoc.xml"
InBlock.gif
InBlock.gif
string xmlText = "<root><para>some para text</para></root>"
InBlock.gif
InBlock.gifDomOperation DomOper 
= new DomOperation(); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
try 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
InBlock.gif
this.textBox1.Text = DomOper.Save(xmlText,savePath); 
InBlock.gif
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gif
catch(XmlException xmlExp) 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
InBlock.gifMessageBox.Show(xmlExp.ToString(),
"Error",MessageBoxButtons.OK,MessageBoxIcon.Error); 
InBlock.gif
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gif
catch(Exception exp) 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
InBlock.gifMessageBox.Show(exp.ToString(),
"Error",MessageBoxButtons.OK,MessageBoxIcon.Error); 
InBlock.gif
ExpandedSubBlockEnd.gif}
 
InBlock.gif
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
private void button4_Click(object sender, System.EventArgs e) 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
InBlock.gif
string savePath = "../../rootDoc2.xml"
InBlock.gif
InBlock.gif
string xmlText = "<?xml version='1.0' encoding='utf-8' ?><root><para>some para text</para></root>"
InBlock.gif
InBlock.gifDomOperation DomOper 
= new DomOperation(); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
try 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
InBlock.gif
this.textBox1.Text = DomOper.SaveByXmlWriter(xmlText,savePath); 
InBlock.gif
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gif
catch(XmlException xmlExp) 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
InBlock.gifMessageBox.Show(xmlExp.ToString(),
"Error",MessageBoxButtons.OK,MessageBoxIcon.Error); 
InBlock.gif
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gif
catch(Exception exp) 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
InBlock.gifMessageBox.Show(exp.ToString(),
"Error",MessageBoxButtons.OK,MessageBoxIcon.Error); 
InBlock.gif
ExpandedSubBlockEnd.gif}
 
InBlock.gif
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
private void button5_Click(object sender, System.EventArgs e) 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
InBlock.gifDomOperation DomOper 
= new DomOperation(); 
InBlock.gif
InBlock.gifXmlDocument xmlDoc 
= new XmlDocument(); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
try 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
InBlock.gifxmlDoc.Load(
this.xmlPath); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
// 创建一个Xml文档片段 
InBlock.gif

InBlock.gifXmlDocumentFragment xmlDocFrag 
= xmlDoc.CreateDocumentFragment(); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
// 创建父根元素节点test:Product 
InBlock.gif

InBlock.gifXmlElement proEle 
= xmlDoc.CreateElement("test","Product","uri:test"); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
// 创建属性并添加到根元素test:Product中 
InBlock.gif

InBlock.gifXmlAttribute proIdAtt 
= xmlDoc.CreateAttribute("ProductID"); 
InBlock.gif
InBlock.gifproIdAtt.Value 
= "MU78"
InBlock.gif
InBlock.gifproEle.Attributes.SetNamedItem(proIdAtt); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
// 创建根元素节点的叶节点 
InBlock.gif

InBlock.gifXmlElement colorEle 
= xmlDoc.CreateElement("color"); 
InBlock.gif
InBlock.gifcolorEle.InnerText 
= "red"
InBlock.gif
InBlock.gifproEle.AppendChild(colorEle); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gifXmlElement sizeEle 
= xmlDoc.CreateElement("size"); 
InBlock.gif
InBlock.gifsizeEle.InnerText 
= "M"
InBlock.gif
InBlock.gifproEle.AppendChild(sizeEle); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gifXmlElement priceEle 
= xmlDoc.CreateElement("price"); 
InBlock.gif
InBlock.gifpriceEle.InnerText 
= "125.99"
InBlock.gif
InBlock.gifproEle.AppendChild(priceEle); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
// 将根元素添加进Xml文档片段中 
InBlock.gif

InBlock.gifxmlDocFrag.AppendChild(proEle); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
this.textBox1.Text = DomOper.AddChildNode(xmlDoc,xmlDocFrag,"//ProductFamily"); 
InBlock.gif
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gif
catch(Exception exp) 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
InBlock.gifMessageBox.Show(exp.ToString(),
"Error",MessageBoxButtons.OK,MessageBoxIcon.Error); 
InBlock.gif
ExpandedSubBlockEnd.gif}
 
InBlock.gif
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
private void button6_Click(object sender, System.EventArgs e) 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
InBlock.gifXmlDocument xmlDoc 
= new XmlDocument(); 
InBlock.gif
InBlock.gifDomOperation DomOper 
= new DomOperation(); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
try 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
InBlock.gifxmlDoc.Load(
"../../sample.xml"); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gifXmlDocumentFragment xmlDocFrag 
= xmlDoc.CreateDocumentFragment(); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gifXmlElement proEle 
= xmlDoc.CreateElement("test","Product","uri:test"); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gifXmlAttribute proIdAtt 
= xmlDoc.CreateAttribute("ProductID"); 
InBlock.gif
InBlock.gifproIdAtt.Value 
= "MU78"
InBlock.gif
InBlock.gifproEle.Attributes.SetNamedItem(proIdAtt); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gifXmlElement colorEle 
= xmlDoc.CreateElement("color"); 
InBlock.gif
InBlock.gifcolorEle.InnerText 
= "red"
InBlock.gif
InBlock.gifproEle.AppendChild(colorEle); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gifXmlElement sizeEle 
= xmlDoc.CreateElement("size"); 
InBlock.gif
InBlock.gifsizeEle.InnerText 
= "M"
InBlock.gif
InBlock.gifproEle.AppendChild(sizeEle); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gifXmlElement priceEle 
= xmlDoc.CreateElement("price"); 
InBlock.gif
InBlock.gifpriceEle.InnerText 
= "125.99"
InBlock.gif
InBlock.gifproEle.AppendChild(priceEle); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gifxmlDocFrag.AppendChild(proEle); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
this.textBox1.Text = DomOper.ReplaceChildNode(xmlDoc,xmlDocFrag,"//ProductFamily",0); 
InBlock.gif
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gif
catch(Exception exp) 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
InBlock.gifMessageBox.Show(exp.ToString(),
"Error",MessageBoxButtons.OK,MessageBoxIcon.Error); 
InBlock.gif
ExpandedSubBlockEnd.gif}
 
InBlock.gif
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
// 使用InnerXml和InnerText直接修改Xml文档 
InBlock.gif

InBlock.gif
private void button7_Click(object sender, System.EventArgs e) 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
InBlock.gifXmlDocument xmlDoc 
= new XmlDocument(); 
InBlock.gif
InBlock.gifxmlDoc.Load(
"../../sample.xml"); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gifXmlNodeList nodeList 
= xmlDoc.SelectNodes("//price"); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
foreach (XmlNode node in nodeList) 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
InBlock.gifnode.InnerXml 
= "<currency type='dollar'>" + node.InnerText + "</currency>"
InBlock.gif
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
this.textBox1.Text = xmlDoc.OuterXml; 
InBlock.gif
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
private void button8_Click(object sender, System.EventArgs e) 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
InBlock.gifDomOperation DomOper 
= new DomOperation(this.xmlPath); 
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
try 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
InBlock.gif
this.textBox1.Text = DomOper.RemoveChildNode("Company","ProductFamily"); 
InBlock.gif
ExpandedSubBlockEnd.gif}
 
InBlock.gif
InBlock.gif
catch(Exception exp) 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif
InBlock.gif
InBlock.gifMessageBox.Show(exp.ToString(),
"Error",MessageBoxButtons.OK,MessageBoxIcon.Error); 
InBlock.gif
ExpandedSubBlockEnd.gif}
 
InBlock.gif
ExpandedSubBlockEnd.gif}
 
InBlock.gif
ExpandedSubBlockEnd.gif}
 
InBlock.gif
ExpandedBlockEnd.gif}
 
None.gif

转载于:https://www.cnblogs.com/mjgforever/archive/2007/05/31/766732.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值