Delphi中封装了几个操作XML类很好用,可C#中却没有,本人参照Delphi封装XML的方法,在C#中进其进行二次封装,贴出来,你用一下,
using System;
using System.Text;
using System.Xml;
using System.IO;
namespace BEA.Model
{
//--------------------------------------------------------------------------------
/// <summary>
/// 结点集合类,多用于访问子结点
/// </summary>
public class XMLNodes
{
private XmlDocument xmlDoc;
private XmlNodeList nodeList;
public XMLNodes(XmlDocument xmldoc, XmlNodeList nodelist)
{
this.xmlDoc = xmldoc;
this.nodeList = nodelist;
}
//private int count;
/// <summary>
/// 子结点个数
/// </summary>
public int Count
{
get
{
return nodeList.Count;
}
}
/// <summary>
/// 获取指定下标的子结点
/// </summary>
/// <param name="index">下标编号,从0开始</param>
/// <returns></returns>
public XMLNode this[int index]
{
get
{
if (index >= this.nodeList.Count)
{
return null;
}
else
{
if (nodeList[index] is XmlElement)
return new XMLNode(this.xmlDoc, (XmlElement)nodeList[index]);
else
return null;
}
}
}
/// <summary>
/// 获取指定Key属性值的子结点,特别说明,用此功能要有KEY属性<ITEM KEY="NO" VALUE="104545"/>
/// </summary>
/// <param name="sKey"></param>
/// <returns></returns>
public XMLNode this[string sKey]
{
get
{
XmlElement xmlNode = null;
XMLNode node = null;
for (int i = 0;
i < nodeList.Count;
i++)
{
xmlNode = (XmlElement)nodeList;
if ((xmlNode.HasAttribute("KEY")) &&
(xmlNode.GetAttribute("KEY") == sKey))
{
node = new XMLNode(xmlDoc, xmlNode);
break;
}
}
return node;
}
}
}
//--------------------------------------------------------------------------------
//操作结点
/// <summary>
/// XML结点
/// </summary>
public class XMLNode
{
private XmlDocument xmlDoc;
private XmlElement xmlNode;
/// <summary>
/// 子结点集合
/// </summary>
private XMLNodes childNodes;
/// <summary>
/// 本结点对应的属性集合
/// </summary>
private XMLAttributes attributes;
/// <summary>
///
/// </summary>
/// <param name="xmldoc"></param>
/// <param name="xmlnode"></param>
public XMLNode(XmlDocument xmldoc, XmlElement xmlnode)
{
this.xmlDoc = xmldoc;
this.xmlNode = xmlnode;
attributes = new XMLAttributes(xmldoc, xmlnode);
childNodes = new XMLNodes(this.xmlDoc, this.xmlNode.ChildNodes);
}
/// <summary>
/// 取当前结点的下一结点
/// </summary>
public XMLNode NextSibling
{
get
{
if (null != xmlNode.NextSibling)
{
return new XMLNode(xmlDoc, (XmlElement)xmlNode.NextSibling);
}
else
{
return null;
}
}
}
/// <summary>
/// 本结点的属性集合
/// </summary>
public XMLAttributes Attributes
{
get
{
return attributes;
}
}
/// <summary>
/// 本结点对应的子结点
/// </summary>
public XMLNodes ChildNodes
{
get
{
return this.childNodes;
}
}
public XmlNode Node
{
get
{
return xmlNode;
}
}
/// <summary>
/// 结点的名字
/// </summary>
public string Name
{
get
{
return this.xmlNode.Name;
}
}
/// <summary>
/// 结点的值
/// </summary>
public string Value
{
get
{
return this.xmlNode.InnerText;
}
set
{
this.xmlNode.InnerText = value;
}
}
#region 操作结点
/// <summary>
/// 添加子结点
/// </summary>
/// <param name="nodeName"></param>
/// <returns></returns>
public XMLNode AddChild(string nodeName)
{
XMLNode aNode = null;
try
{
XmlElement node = (XmlElement)this.xmlDoc.CreateNode(XmlNodeType.Element, nodeName, "");
this.xmlNode.AppendChild(node);
aNode = new XMLNode(xmlDoc, node);
}
catch
{
}
return aNode;
}
/// <summary>
/// 添加子结点并赋值
/// </summary>
/// <param name="nodeName"></param>
/// <param name="sValue"></param>
/// <returns></returns>
public XMLNode AddChild(string nodeName, string sValue)
{
XMLNode aNode = null;
try
{
XmlElement node = (XmlElement)this.xmlDoc.CreateNode(XmlNodeType.Element, nodeName, "");
node.Value = sValue;
this.xmlNode.AppendChild(node);
aNode = new XMLNode(xmlDoc, node);
}
catch
{
}
return aNode;
}
/// <summary>
/// 本结点是否有子结点
/// </summary>
/// <returns></returns>
public bool HasChildNodes()
{
return this.xmlNode.ChildNodes.Count > 0;
}
/// <summary>
/// 是否在指定的属性
/// </summary>
/// <param name="attriubteName"></param>
/// <returns></returns>
public bool HasAttribute(string attriubteName)
{
return this.xmlNode.HasAttribute(attriubteName);
}
#endregion
}
//-------------------------------------------------------------------------------
//操作属性
public class XMLAttributes
{
private XmlDocument xmlDoc;
private XmlElement xmlNode;
public XMLAttributes(XmlDocument xmldoc, XmlElement xmlnode)
{
this.xmlDoc = xmldoc;
this.xmlNode = xmlnode;
}
public string this[string attriName]
{
get
{
//判别属性存不存在
if (this.xmlNode.HasAttribute(attriName))
return this.xmlNode.GetAttribute(attriName);
else
//返回属性值
return "";
}
set
{
if (this.xmlNode.HasAttribute(attriName))
{
xmlNode.Attributes[attriName].Value = value;
}
else
{
XmlAttribute xmlAttri = this.xmlDoc.CreateAttribute(attriName);
xmlAttri.Value = value;
xmlNode.Attributes.Append(xmlAttri);
}
}
}
public string this[int index]
{
get
{
//判别属性存不存在
if (index >= this.xmlNode.Attributes.Count)
{
return "";
}
else
{
return this.xmlNode.Attributes[index].Value;
}
}
}
/// <summary>
/// 移除指定的属性
/// </summary>
/// <param name="sName"></param>
public void Remove(string attriName)
{
if (this.xmlNode.HasAttribute(attriName))
{
XmlAttribute xmlAttri = xmlNode.GetAttributeNode(attriName);
if (null != xmlAttri)
{
xmlNode.Attributes.Remove(xmlAttri);
}
}
}
/// <summary>
/// 清除所有的属性
/// </summary>
public void Clear()
{
xmlNode.Attributes.RemoveAll();
}
}
//------------------------------------------------------------------------------------------
//XML文档类
public class XMLDocument
{
/// <summary>
/// XML包字串
/// </summary>
private string sXML;
private XmlDocument xmlDoc;
private XMLNode aRoot;
public XMLDocument()
{
sXML = "<?xml version=/"1.0/" encoding=/"utf-16/" ?> <XML></XML>";
LoadXMLDoc();
}
public XMLDocument(string sxml)
{
sXML = sxml;
LoadXMLDoc();
}
private bool LoadXMLDoc()
{
xmlDoc = new XmlDocument();
xmlDoc.LoadXml(sXML);
aRoot = new XMLNode(xmlDoc, (XmlElement)xmlDoc.FirstChild.NextSibling);
return true;
}
/// <summary>
/// 从文件加载XML包
/// </summary>
/// <param name="sFileName"></param>
/// <returns></returns>
public bool LoadXMLDoc(string sFileName, out string sError)
{
sError = "";
if (!File.Exists(sFileName))
{
sError = sFileName + "文件不存在";
return false;
}
try
{
xmlDoc = new XmlDocument();
xmlDoc.Load(sFileName);
aRoot = new XMLNode(xmlDoc, (XmlElement)xmlDoc.FirstChild.NextSibling);
return true;
}
catch (Exception ex)
{
BEA.Model.Log log = new Log();
log.Write(ex);
sError = ex.Message;
}
return false;
}
public void Clear()
{
sXML = "<?xml version=/"1.0/" encoding=/"utf-16/" ?> <XML></XML>";
LoadXMLDoc();
}
public bool Save(string sFileName, out string sError)
{
sError = "";
//判别目录不存在
string sPath = Path.GetDirectoryName(sFileName);
if (!Directory.Exists(sPath))
{
sError = sPath + "不存在";
return false;
}
this.xmlDoc.Save(sFileName);
return true;
}
#region 属性
public XMLNode RootNode
{
get
{
return this.aRoot;
}
}
/// <summary>
/// 取XML包串
/// </summary>
public string SXML
{
get
{
StringWriter sw = new StringWriter();
xmlDoc.Save(sw);
return sw.ToString();
}
set
{
this.sXML = value;
LoadXMLDoc();
}
}
#endregion
}
}