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

         }
}

posted on 2010-10-20 17:16  大江  阅读(152)  评论(0编辑  收藏  举报