XML操作类

操作类1:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Xml;
using System.Text;
using MSXML2;


namespace EC
{
    /// <summary>
    /// XML 操作基类
    /// </summary>
    public class XmlObject : IDisposable
    {      
        
        #region 读取XML到DataSet
        /**************************************************
         * 函数名称:GetXml(string XmlPath)
         * 功能说明:读取XML到DataSet
         * 参    数:XmlPath:xml文档路径
         * 使用示列:
         *          using EC; //引用命名空间
         *          string xmlPath = Server.MapPath("/EBDnsConfig/DnsConfig.xml"); //获取xml路径
         *          DataSet ds = EC.XmlObject.GetXml(xmlPath); //读取xml到DataSet中
         ************************************************/
        /// <summary>
        /// 功能:读取XML到DataSet中
        /// </summary>
        /// <param name="XmlPath">xml路径</param>
        /// <returns>DataSet</returns>
        public static DataSet GetXml(string XmlPath)
        {
            DataSet ds = new DataSet();
            ds.ReadXml(@XmlPath);
            return ds;
        }
        #endregion

        #region 读取xml文档并返回一个节点
        /**************************************************
         * 函数名称:ReadXmlReturnNode(string XmlPath,string Node)
         * 功能说明:读取xml文档并返回一个节点:适用于一级节点
         * 参    数: XmlPath:xml文档路径;Node 返回的节点名称
         * 适应用Xml:<?xml version="1.0" encoding="utf-8" ?>
         *           <root>
         *               <dns1>ns1.everdns.com</dns1>
         *          </root>
         * 使用示列:
         *          using EC; //引用命名空间
         *          string xmlPath = Server.MapPath("/EBDnsConfig/DnsConfig.xml"); //获取xml路径
         *          Response.Write(XmlObject.ReadXmlReturnNode(xmlPath, "mailmanager"));
         ************************************************/
        /// <summary>
        /// 读取xml文档并返回一个节点:适用于一级节点
        /// </summary>
        /// <param name="XmlPath">xml路径</param>
        /// <param name="NodeName">节点</param>
        /// <returns></returns>
        public static string ReadXmlReturnNode(string XmlPath,string Node)
        {
            XmlDocument docXml = new XmlDocument();
            docXml.Load(@XmlPath);
            XmlNodeList xn = docXml.GetElementsByTagName(Node);
            return xn.Item(0).InnerText.ToString();          
        }
        #endregion

        #region 查找数据,返回一个DataSet
        /**************************************************
         * 函数名称:GetXmlData(string xmlPath, string XmlPathNode)
         * 功能说明:查找数据,返回当前节点的所有下级节点,填充到一个DataSet中
         * 参    数:xmlPath:xml文档路径;XmlPathNode:当前节点的路径
         * 使用示列:
         *          using EC; //引用命名空间
         *          string xmlPath = Server.MapPath("/EBDomainConfig/DomainConfig.xml"); //获取xml路径
         *          DataSet ds = new DataSet();
         *          ds = XmlObject.GetXmlData(xmlPath, "root/items");//读取当前路径
         *          this.GridView1.DataSource = ds;
         *          this.GridView1.DataBind();
         *          ds.Clear();
         *          ds.Dispose();
         * Xml示例:
         *         <?xml version="1.0" encoding="utf-8" ?>
         *            <root>
         *              <items name="xinnet">
         *                <url>http://www.paycenter.com.cn/cgi-bin/</url>
         *                <port>80</port>
         *              </items>
         *            </root>
         ************************************************/
        /// <summary>
        /// 查找数据,返回当前节点的所有下级节点,填充到一个DataSet中
        /// </summary>
        /// <param name="xmlPath">xml文档路径</param>
        /// <param name="XmlPathNode">节点的路径:根节点/父节点/当前节点</param>
        /// <returns></returns>
        public static DataSet GetXmlData(string xmlPath, string XmlPathNode)
        {
            XmlDocument objXmlDoc = new XmlDocument();
            objXmlDoc.Load(xmlPath);
            DataSet ds = new DataSet();
            StringReader read = new StringReader(objXmlDoc.SelectSingleNode(XmlPathNode).OuterXml);
            ds.ReadXml(read);
            return ds;       
        }


        #endregion

        #region 更新Xml节点内容
        /**************************************************
         * 函数名称:XmlNodeReplace(string xmlPath,string Node,string Content)
         * 功能说明:更新Xml节点内容
         * 参    数:xmlPath:xml文档路径;Node:当前节点的路径;Content:内容
         * 使用示列:
         *          using EC; //引用命名空间
         *          string xmlPath = Server.MapPath("/EBDomainConfig/DomainConfig.xml"); //获取xml路径
         *          XmlObject.XmlNodeReplace(xmlPath, "root/test", "56789");  //更新节点内容
         ************************************************/
        /// <summary>
        /// 更新Xml节点内容
        /// </summary>
        /// <param name="xmlPath">xml路径</param>
        /// <param name="Node">要更换内容的节点:节点路径根节点/父节点/当前节点</param>
        /// <param name="Content">新的内容</param>
        public static void XmlNodeReplace(string xmlPath,string Node,string Content)
        {
            XmlDocument objXmlDoc = new XmlDocument();
            objXmlDoc.Load(xmlPath);
            objXmlDoc.SelectSingleNode(Node).InnerText = Content;
            objXmlDoc.Save(xmlPath);
           
        }

        #endregion

        #region 删除XML节点和此节点下的子节点
        /**************************************************
         * 函数名称:XmlNodeDelete(string xmlPath,string Node)
         * 功能说明:删除XML节点和此节点下的子节点
         * 参    数:xmlPath:xml文档路径;Node:当前节点的路径;
         * 使用示列:
         *          using EC; //引用命名空间
         *          string xmlPath = Server.MapPath("/EBDomainConfig/DomainConfig.xml"); //获取xml路径
         *          XmlObject.XmlNodeDelete(xmlPath, "root/test");  //删除当前节点
         ************************************************/
        /// <summary>
        /// 删除XML节点和此节点下的子节点
        /// </summary>
        /// <param name="xmlPath">xml文档路径</param>
        /// <param name="Node">节点路径</param>
        public static void XmlNodeDelete(string xmlPath,string Node)
        {
            XmlDocument objXmlDoc = new XmlDocument();
            objXmlDoc.Load(xmlPath);
            string mainNode = Node.Substring(0, Node.LastIndexOf("/"));
            objXmlDoc.SelectSingleNode(mainNode).RemoveChild(objXmlDoc.SelectSingleNode(Node));
            objXmlDoc.Save(xmlPath);
        }

        #endregion

        #region 插入一个节点和此节点的字节点
        /**************************************************
         * 函数名称:XmlInsertNode(string xmlPath, string MailNode, string ChildNode, string Element,string Content)
         * 功能说明:插入一个节点和此节点的字节点
         * 参    数:xmlPath:xml文档路径;MailNode:当前节点的路径;ChildNode:新插入的节点;Element:插入节点的子节点;Content:子节点的内容
         * 使用示列:
         *          using EC; //引用命名空间
         *          string xmlPath = Server.MapPath("/EBDomainConfig/DomainConfig.xml"); //获取xml路径
         *          XmlObject.XmlInsertNode(xmlPath, "root/test","test1","test2","测试内容");  //插入一个节点和此节点的字节点
         * 生成的XML格式为
         *          <test>
         *               <test1>
         *                    <test2>测试内容</test2>
         *                </test1>
         *            </test>
         ************************************************/
        /// <summary>
        /// 插入一个节点和此节点的字节点
        /// </summary>
        /// <param name="xmlPath">xml路径</param>
        /// <param name="MailNode">当前节点路径</param>
        /// <param name="ChildNode">新插入节点</param>
        /// <param name="Element">插入节点的子节点</param>
        /// <param name="Content">子节点的内容</param>
        public static void XmlInsertNode(string xmlPath, string MailNode, string ChildNode, string Element,string Content)
        {
            XmlDocument objXmlDoc = new XmlDocument();
            objXmlDoc.Load(xmlPath);
            XmlNode objRootNode = objXmlDoc.SelectSingleNode(MailNode);
            XmlElement objChildNode = objXmlDoc.CreateElement(ChildNode);
            objRootNode.AppendChild(objChildNode);
            XmlElement objElement = objXmlDoc.CreateElement(Element);
            objElement.InnerText = Content;
            objChildNode.AppendChild(objElement);
            objXmlDoc.Save(xmlPath);
        }

        #endregion

         }
}


操作类2

public class XmlControl
{
protected string strXmlFile;
protected XmlDocument objXmlDoc = new XmlDocument();

public XmlControl(string XmlFile)
{
//
// TODO:
在这里加入建构函式的程序代码
//
try
{
objXmlDoc.Load(XmlFile);
}
catch (System.Exception ex)
{
throw ex;
}
strXmlFile = XmlFile;
}

public DataView GetData(string XmlPathNode)
{
//
查找数据。返回一个 DataView
DataSet ds = new DataSet();
StringReader read = new StringReader(objXmlDoc.SelectSingleNode(XmlPathNode).OuterXml);
ds.ReadXml(read);
return ds.Tables[0].DefaultView;
}

public void Replace(string XmlPathNode,string Content)
{
//
更新节点内容。
objXmlDoc.SelectSingleNode(XmlPathNode).InnerText = Content;
}

public void Delete(string Node)
{
//
删除一个节点。
string mainNode = Node.Substring(0,Node.LastIndexOf("/"));
objXmlDoc.SelectSingleNode(mainNode).RemoveChild(objXmlDoc.SelectSingleNode(Node));
}

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

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

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

public void Save()
{
//
保存文檔。
try
{
objXmlDoc.Save(strXmlFile);
}
catch (System.Exception ex)
{
throw ex;
}
objXmlDoc = null;
}
}

=========================================================

实例应用:

string strXmlFile = Server.MapPath("TestXml.xml");
XmlControl xmlTool = new XmlControl(strXmlFile);

//
数据显视
// dgList.DataSource = xmlTool.GetData("Book/Authors[ISBN=\"0002\"]");
// dgList.DataBind();

//
更新元素内容
// xmlTool.Replace("Book/Authors[ISBN=\"0002\"]/Content","ppppppp");
// xmlTool.Save();

//
添加一个新节点
// xmlTool.InsertNode("Book","Author","ISBN","0004");
// xmlTool.InsertElement("Book/Author[ISBN=\"0004\"]","Content","aaaaaaaaa");
// xmlTool.InsertElement("Book/Author[ISBN=\"0004\"]","Title","Sex","man","iiiiiiii");
// xmlTool.Save();

//
删除一个指定节点的所有内容和属性
// xmlTool.Delete("Book/Author[ISBN=\"0004\"]");
// xmlTool.Save();

//
删除一个指定节点的子节点
// xmlTool.Delete("Book/Authors[ISBN=\"0003\"]");
// xmlTool.Save();

你可能感兴趣的:(xml)