一个简单的XML的操纵类
转一位前辈的代码自己加了点注释,以备自己使用时查找,同时也希望能给需要的人一点帮助.
using System;
using System.Xml;
using System.Text;
using System.IO;
using System.Data;
namespace TestCenter.XMLData
{
/// <summary>
/// XMLHandle XML操作。
/// 示例:
/// string strXmlFile = Server.MapPath("MyXML.xml");
/// XMLHandle xmlHandle = new XmlControl(strXmlFile);
/// DataView dv=xmlHandle.GetData("BackAdmin/User/Name");
///
/// </summary>
public class XMLHandle
{
protected string strXmlFile;//操纵的XML文档路径
protected XmlDocument objXmlDoc = new XmlDocument();
/// <summary>
/// 构造函数
/// </summary>
/// <param name="XmlFile">XML文件的路径 初始化类时指定</param>
public XMLHandle(string XmlFile)
{
try
{
objXmlDoc.Load(XmlFile);
}
catch (System.Exception ex)
{
throw ex;
}
strXmlFile = XmlFile;
}
/// <summary>
/// 查找指定节点的数据 返回DataView
/// </summary>
/// <param name="XmlPathNode"></param>
/// <returns>DataView</returns>
public DataView GetData(string XmlPathNode)
{
DataSet dst = new DataSet();
StringReader read = new StringReader(objXmlDoc.SelectSingleNode(XmlPathNode).OuterXml);
dst.ReadXml(read);
return dst.Tables[0].DefaultView;
}
/// <summary>
/// 更新指定节点的内容
/// </summary>
/// <param name="XmlPathNode">节点</param>
/// <param name="Content">内容</param>
public void Replace(string XmlPathNode,string Content)
{
objXmlDoc.SelectSingleNode(XmlPathNode).InnerText = Content;
}
/// <summary>
/// 删除指定节点
/// </summary>
/// <param name="Node">节点</param>
public void Delete(string Node)
{
string mainNode = Node.Substring(0,Node.LastIndexOf("/"));
objXmlDoc.SelectSingleNode(mainNode).RemoveChild(objXmlDoc.SelectSingleNode(Node));
}
/// <summary>
/// 插入一个节点和此节点的一子节点。
/// </summary>
/// <param name="MainNode">主节点</param>
/// <param name="ChildNode">子节点</param>
/// <param name="Element">元素名</param>
/// <param name="Content">值</param>
public void InsertNode(string MainNode,string ChildNode,string Element,string Content)
{
XmlNode objRootNode = objXmlDoc.SelectSingleNode(MainNode);
XmlElement objChildNode = objXmlDoc.CreateElement(ChildNode);
objRootNode.AppendChild(objChildNode);
XmlElement objElement = objXmlDoc.CreateElement(Element);
objElement.InnerText = Content;
objChildNode.AppendChild(objElement);
}
/// <summary>
/// 插入一节点 包括一个属性
/// </summary>
/// <param name="MainNode"></param>
/// <param name="Element"></param>
/// <param name="Attrib"></param>
/// <param name="AttribContent"></param>
/// <param name="Content"></param>
public void InsertElement(string MainNode,string Element,string Attrib,string AttribContent,string Content)
{
XmlNode objNode = objXmlDoc.SelectSingleNode(MainNode);
XmlElement objElement = objXmlDoc.CreateElement(Element);
objElement.SetAttribute(Attrib,AttribContent);
objElement.InnerText = Content;
objNode.AppendChild(objElement);
}
/// <summary>
/// 插入一节点不带属性
/// </summary>
/// <param name="MainNode"></param>
/// <param name="Element"></param>
/// <param name="Content"></param>
public void InsertElement(string MainNode,string Element,string Content)
{
XmlNode objNode = objXmlDoc.SelectSingleNode(MainNode);
XmlElement objElement = objXmlDoc.CreateElement(Element);
objElement.InnerText = Content;
objNode.AppendChild(objElement);
}
/// <summary>
/// 保存文档
/// </summary>
public void Save()
{
try
{
objXmlDoc.Save(strXmlFile);
}
catch (System.Exception ex)
{
throw new System.Exception(ex.ToString());
}
objXmlDoc = null;
}
}
}
using System.Xml;
using System.Text;
using System.IO;
using System.Data;
namespace TestCenter.XMLData
{
/// <summary>
/// XMLHandle XML操作。
/// 示例:
/// string strXmlFile = Server.MapPath("MyXML.xml");
/// XMLHandle xmlHandle = new XmlControl(strXmlFile);
/// DataView dv=xmlHandle.GetData("BackAdmin/User/Name");
///
/// </summary>
public class XMLHandle
{
protected string strXmlFile;//操纵的XML文档路径
protected XmlDocument objXmlDoc = new XmlDocument();
/// <summary>
/// 构造函数
/// </summary>
/// <param name="XmlFile">XML文件的路径 初始化类时指定</param>
public XMLHandle(string XmlFile)
{
try
{
objXmlDoc.Load(XmlFile);
}
catch (System.Exception ex)
{
throw ex;
}
strXmlFile = XmlFile;
}
/// <summary>
/// 查找指定节点的数据 返回DataView
/// </summary>
/// <param name="XmlPathNode"></param>
/// <returns>DataView</returns>
public DataView GetData(string XmlPathNode)
{
DataSet dst = new DataSet();
StringReader read = new StringReader(objXmlDoc.SelectSingleNode(XmlPathNode).OuterXml);
dst.ReadXml(read);
return dst.Tables[0].DefaultView;
}
/// <summary>
/// 更新指定节点的内容
/// </summary>
/// <param name="XmlPathNode">节点</param>
/// <param name="Content">内容</param>
public void Replace(string XmlPathNode,string Content)
{
objXmlDoc.SelectSingleNode(XmlPathNode).InnerText = Content;
}
/// <summary>
/// 删除指定节点
/// </summary>
/// <param name="Node">节点</param>
public void Delete(string Node)
{
string mainNode = Node.Substring(0,Node.LastIndexOf("/"));
objXmlDoc.SelectSingleNode(mainNode).RemoveChild(objXmlDoc.SelectSingleNode(Node));
}
/// <summary>
/// 插入一个节点和此节点的一子节点。
/// </summary>
/// <param name="MainNode">主节点</param>
/// <param name="ChildNode">子节点</param>
/// <param name="Element">元素名</param>
/// <param name="Content">值</param>
public void InsertNode(string MainNode,string ChildNode,string Element,string Content)
{
XmlNode objRootNode = objXmlDoc.SelectSingleNode(MainNode);
XmlElement objChildNode = objXmlDoc.CreateElement(ChildNode);
objRootNode.AppendChild(objChildNode);
XmlElement objElement = objXmlDoc.CreateElement(Element);
objElement.InnerText = Content;
objChildNode.AppendChild(objElement);
}
/// <summary>
/// 插入一节点 包括一个属性
/// </summary>
/// <param name="MainNode"></param>
/// <param name="Element"></param>
/// <param name="Attrib"></param>
/// <param name="AttribContent"></param>
/// <param name="Content"></param>
public void InsertElement(string MainNode,string Element,string Attrib,string AttribContent,string Content)
{
XmlNode objNode = objXmlDoc.SelectSingleNode(MainNode);
XmlElement objElement = objXmlDoc.CreateElement(Element);
objElement.SetAttribute(Attrib,AttribContent);
objElement.InnerText = Content;
objNode.AppendChild(objElement);
}
/// <summary>
/// 插入一节点不带属性
/// </summary>
/// <param name="MainNode"></param>
/// <param name="Element"></param>
/// <param name="Content"></param>
public void InsertElement(string MainNode,string Element,string Content)
{
XmlNode objNode = objXmlDoc.SelectSingleNode(MainNode);
XmlElement objElement = objXmlDoc.CreateElement(Element);
objElement.InnerText = Content;
objNode.AppendChild(objElement);
}
/// <summary>
/// 保存文档
/// </summary>
public void Save()
{
try
{
objXmlDoc.Save(strXmlFile);
}
catch (System.Exception ex)
{
throw new System.Exception(ex.ToString());
}
objXmlDoc = null;
}
}
}