C# XML DOM 实现
LoadXML按纽用于加载XML文档,LoadXMLReader按纽使用XmlTextReader加载文档,SaveXML按纽保存文档,SaveXMLWriter按纽将文档保存到XmlTextWriter中,Add Product按纽添加节点,Replace Product按纽替换节点,Change Order按纽修改文档,Remove Product Info按纽移除节点。 


DomOperation类封装了所有按纽功能的实现,代码如下: 
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()方法用于保存文档。 
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; 


DomOperation 构造器 


DomOperation 资源释放方法 


DomOperation 属性 


/// <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 ); 
} 


Windows 窗体设计器生成的代码 


/// <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); 
} 
} 
} 
} 
posted on 2007-05-31 17:44 mjgforever 阅读(735) 评论(0) 收藏 举报


浙公网安备 33010602011771号