XmlHelper

using System;
using System.Xml;
using System.Web;

namespace XXXX.Common
{
    /// 
    /// 必需用XPATH表达式来获取相应节点
    /// 关于xpath可以参见:
    /// 
    public class XmlHelper
    {
        #region 变量
        /// 
        /// xml文件所在路径类型
        /// 
        /// xml文件所在路径类型
        public enum enumXmlPathType
        {
            /// 
            /// 绝对路径
            /// 
            AbsolutePath,
            /// 
            /// 虚拟路径
            /// 
            VirtualPath
        }

        private string xmlFilePath;
        private enumXmlPathType xmlFilePathType;
        private XmlDocument xmlDoc = new XmlDocument();
        #endregion

        #region 属性
        /// 
        /// 文件路径
        /// 
        /// 文件路径
        public string XmlFilePath
        {
            get
            {
                return this.xmlFilePath;
            }
            set
            {
                xmlFilePath = value;
            }
        }
        /// 
        /// 文件路径类型
        /// 
        public enumXmlPathType XmlFilePathTyp
        {
            set
            {
                xmlFilePathType = value;
            }
        }
        #endregion

        #region 构造函数
        /// 
        ///
        /// 
        /// 
        public XmlHelper(string tempXmlFilePath)
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //

            this.xmlFilePathType = enumXmlPathType.VirtualPath;
            this.xmlFilePath = tempXmlFilePath;
            GetXmlDocument();
            //xmlDoc.Load( xmlFilePath ) ;
        }

        /// 
        /// 构造函数
        /// 
        /// 文件路径
        /// 类型
        public XmlHelper(string tempXmlFilePath, enumXmlPathType tempXmlFilePathType)
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
            this.xmlFilePathType = tempXmlFilePathType;
            this.xmlFilePath = tempXmlFilePath;
            GetXmlDocument();
        }
        #endregion

        
        ///获取XmlDocument实体类
        ///   
        /// 指定的XML描述文件的一个xmldocument实例
        private XmlDocument GetXmlDocument()
        {
            XmlDocument doc = null;
            if (this.xmlFilePathType == enumXmlPathType.AbsolutePath)
            {
                doc = GetXmlDocumentFromFile(xmlFilePath);
            }
            else if (this.xmlFilePathType == enumXmlPathType.VirtualPath)
            {
                doc = GetXmlDocumentFromFile(HttpContext.Current.Server.MapPath(xmlFilePath));
            }
            return doc;
        }

        private XmlDocument GetXmlDocumentFromFile(string tempXmlFilePath)
        {
            string xmlFileFullPath = tempXmlFilePath;
            xmlDoc.Load(xmlFileFullPath);
            //定义事件处理
            xmlDoc.NodeChanged += new XmlNodeChangedEventHandler(this.nodeUpdateEvent);
            xmlDoc.NodeInserted += new XmlNodeChangedEventHandler(this.nodeInsertEvent);
            xmlDoc.NodeRemoved += new XmlNodeChangedEventHandler(this.nodeDeleteEvent);
            return xmlDoc;
        }

        #region 读取指定节点的指定属性值
        /// 
        /// 功能:
        /// 读取指定节点的指定属性值   
        /// 
        /// 节点名称
        /// 此节点的属性
        /// 
        public string GetXmlNodeAttributeValue(string strNode, string strAttribute)
        {
            string strReturn = "";
            try
            {
                //根据指定路径获取节点
                XmlNode xmlNode = xmlDoc.SelectSingleNode(strNode);
                if (!(xmlNode == null))
                {//获取节点的属性,并循环取出需要的属性值
                    XmlAttributeCollection xmlAttr = xmlNode.Attributes;

                    for (int i = 0; i < xmlAttr.Count; i++)
                    {
                        if (xmlAttr.Item(i).Name == strAttribute)
                        {
                            strReturn = xmlAttr.Item(i).Value;
                            break;
                        }
                    }
                }
            }
            catch (XmlException xmle)
            {
                throw xmle;
            }
            return strReturn;
        }
        #endregion

        #region 读取指定节点的值
        /// 
        /// 功能:
        /// 读取指定节点的值   
        /// 
        /// 节点名称
        /// 
        public string GetXmlNodeValue(string strNode)
        {
            string strReturn = String.Empty;

            try
            {
                //根据路径获取节点
                XmlNode xmlNode = xmlDoc.SelectSingleNode(strNode);
                if (!(xmlNode == null))
                    strReturn = xmlNode.InnerText;
            }
            catch (XmlException xmle)
            {
                throw xmle;
            }
            return strReturn;
        }
        #endregion

        #region 设置节点值
        /// 
        /// 功能:
        /// 设置节点值       
        /// 
        /// 节点的名称
        /// 节点值
        public void SetXmlNodeValue(string xmlNodePath, string xmlNodeValue)
        {
            try
            {
                //可以批量为符合条件的节点进行付值
                XmlNodeList xmlNode = this.xmlDoc.SelectNodes(xmlNodePath);
                if (!(xmlNode == null))
                {
                    foreach (XmlNode xn in xmlNode)
                    {
                        xn.InnerText = xmlNodeValue;
                    }
                }

                /* 根据指定路径获取节点
                XmlNode xmlNode = xmlDoc.SelectSingleNode(xmlNodePath) ;           
                //设置节点值
                if (!(xmlNode==null))
                    xmlNode.InnerText = xmlNodeValue ;*/
            }
            catch (XmlException xmle)
            {
                throw xmle;
            }
        }
        #endregion

        #region 设置节点的属性值
        /// 
        /// 功能:
        /// 设置节点的属性值   
        /// 
        /// 节点名称
        /// 属性名称
        /// 属性值
        public void SetXmlNodeAttributeValue(string xmlNodePath, string xmlNodeAttribute, string xmlNodeAttributeValue)
        {
            try
            {
                //可以批量为符合条件的节点的属性付值
                XmlNodeList xmlNode = this.xmlDoc.SelectNodes(xmlNodePath);
                if (!(xmlNode == null))
                {
                    foreach (XmlNode xn in xmlNode)
                    {
                        XmlAttributeCollection xmlAttr = xn.Attributes;
                        for (int i = 0; i < xmlAttr.Count; i++)
                        {
                            if (xmlAttr.Item(i).Name == xmlNodeAttribute)
                            {
                                xmlAttr.Item(i).Value = xmlNodeAttributeValue;
                                break;
                            }
                        }
                    }
                }
                /*单个节点
               //根据指定路径获取节点
               XmlNode xmlNode = xmlDoc.SelectSingleNode(xmlNodePath) ;
               if (!(xmlNode==null))
               {//获取节点的属性,并循环取出需要的属性值
                   XmlAttributeCollection xmlAttr = xmlNode.Attributes ;
                   for(int i=  ; i
        /// 获取XML文件的根元素
        /// 
        public XmlNode GetXmlRoot()
        {
            return xmlDoc.DocumentElement;
        }

        /// 
        /// 在根节点下添加父节点
        /// 
        public void AddParentNode(string parentNode)
        {
            try
            {
                XmlNode root = GetXmlRoot();
                XmlNode parentXmlNode = xmlDoc.CreateElement(parentNode);
                root.AppendChild(parentXmlNode);
            }
            catch (XmlException xmle)
            {
                throw xmle;
            }
        }

        /// 
        /// 向一个已经存在的父节点中插入一个子节点
        /// 
        /// 父节点
        /// 字节点名称
        public void AddChildNode(string parentNodePath, string childnodename)
        {
            try
            {
                XmlNode parentXmlNode = xmlDoc.SelectSingleNode(parentNodePath);
                if (!((parentXmlNode) == null))//如果此节点存在
                {
                    XmlNode childXmlNode = xmlDoc.CreateElement(childnodename);
                    parentXmlNode.AppendChild(childXmlNode);
                }
                else
                {//如果不存在就放父节点添加
                    //this.GetXmlRoot().AppendChild(childXmlNode);
                }

            }
            catch (XmlException xmle)
            {
                throw xmle;
            }
        }

        /// 
        /// 向一个节点添加属性
        /// 
        /// 节点路径
        /// 属性名
        public void AddAttribute(string NodePath, string NodeAttribute)
        {
            privateAddAttribute(NodePath, NodeAttribute, "");
        }
        /// 
        ///
        /// 
        /// 
        /// 
        /// 
        private void privateAddAttribute(string NodePath, string NodeAttribute, string NodeAttributeValue)
        {
            try
            {
                XmlNode nodePath = xmlDoc.SelectSingleNode(NodePath);
                if (!(nodePath == null))
                {
                    XmlAttribute nodeAttribute = this.xmlDoc.CreateAttribute(NodeAttribute);
                    nodeAttribute.Value = NodeAttributeValue;
                    nodePath.Attributes.Append(nodeAttribute);
                }
            }
            catch (XmlException xmle)
            {
                throw xmle;
            }
        }
        /// 
        ///  向一个节点添加属性,并付值
        /// 
        /// 节点
        /// 属性名
        /// 属性值
        public void AddAttribute(string NodePath, string NodeAttribute, string NodeAttributeValue)
        {
            privateAddAttribute(NodePath, NodeAttribute, NodeAttributeValue);
        }
        #endregion

        #region 删除
        /// 
        /// 删除节点的一个属性
        /// 
        /// 节点所在的xpath表达式
        /// 属性名
        public void DeleteAttribute(string NodePath, string NodeAttribute)
        {
            XmlNodeList nodePath = this.xmlDoc.SelectNodes(NodePath);
            if (!(nodePath == null))
            {
                foreach (XmlNode tempxn in nodePath)
                {
                    XmlAttributeCollection xmlAttr = tempxn.Attributes;
                    for (int i = 0; i < xmlAttr.Count; i++)
                    {
                        if (xmlAttr.Item(i).Name == NodeAttribute)
                        {
                            tempxn.Attributes.RemoveAt(i);
                            break;
                        }
                    }
                }
            }
        }

        /// 
        /// 删除节点,当其属性值等于给定的值时
        /// 
        /// 节点所在的xpath表达式
        /// 属性
        /// 值
        public void DeleteAttribute(string NodePath, string NodeAttribute, string NodeAttributeValue)
        {
            XmlNodeList nodePath = this.xmlDoc.SelectNodes(NodePath);
            if (!(nodePath == null))
            {
                foreach (XmlNode tempxn in nodePath)
                {
                    XmlAttributeCollection xmlAttr = tempxn.Attributes;
                    for (int i = 0; i < xmlAttr.Count; i++)
                    {
                        if (xmlAttr.Item(i).Name == NodeAttribute && xmlAttr.Item(i).Value == NodeAttributeValue)
                        {
                            tempxn.Attributes.RemoveAt(i);
                            break;
                        }
                    }
                }
            }
        }
        /// 
        /// 删除节点
        /// 
        /// 
        /// 
        public void DeleteXmlNode(string tempXmlNode)
        {
            XmlNodeList nodePath = this.xmlDoc.SelectNodes(tempXmlNode);
            if (!(nodePath == null))
            {
                foreach (XmlNode xn in nodePath)
                {
                    xn.ParentNode.RemoveChild(xn);
                }
            }
        }

        #endregion

        #region XML文档事件
        /// 
        ///
        /// 
        /// 
        /// 
        private void nodeInsertEvent(Object src, XmlNodeChangedEventArgs args)
        {
            //保存设置
            SaveXmlDocument();
        }
        /// 
        ///
        /// 
        /// 
        /// 
        private void nodeDeleteEvent(Object src, XmlNodeChangedEventArgs args)
        {
            //保存设置
            SaveXmlDocument();
        }
        /// 
        ///
        /// 
        /// 
        /// 
        private void nodeUpdateEvent(Object src, XmlNodeChangedEventArgs args)
        {
            //保存设置
            SaveXmlDocument();
        }
        #endregion

        #region 保存XML文件
        /// 
        /// 功能:
        /// 保存XML文件
        ///
        /// 
        public void SaveXmlDocument()
        {
            try
            {
                //保存设置的结果
                if (this.xmlFilePathType == enumXmlPathType.AbsolutePath)
                {
                    Savexml(xmlFilePath);
                }
                else if (this.xmlFilePathType == enumXmlPathType.VirtualPath)
                {
                    Savexml(HttpContext.Current.Server.MapPath(xmlFilePath));
                }
            }
            catch (XmlException xmle)
            {
                throw xmle;
            }
        }

        /// 
        /// 功能:
        /// 保存XML文件   
        /// 
        public void SaveXmlDocument(string tempXMLFilePath)
        {
            try
            {
                //保存设置的结果
                Savexml(tempXMLFilePath);
            }
            catch (XmlException xmle)
            {
                throw xmle;
            }
        }
        /// 
        ///
        /// 
        /// 
        private void Savexml(string filepath)
        {
            xmlDoc.Save(filepath);
        }

        #endregion
    }
}

你可能感兴趣的:(C#,asp.net,.net,xml)