XMLHelper.cs

http://yunpan.cn/Q7czcYTwE8qkc  提取碼 545c


using System;

using System.Linq;
using System.Xml.Linq;
using System.Xml;
using System.Data;
using System.Configuration;
/// 這個是用VS2010寫的,如果用VS2005,請去掉System.Linq和System.Xml.Linq的引用
/// 可以將此文件直接編譯成dll,今後程序只需要引用該dll後開頭添加using XmlLibrary;即可。
namespace XmlLibrary
{
    ///
    /// XMLHelper參數
    ///
    public class XmlParameter
    {
        private string _name;
        private string _innerText;
        private string _namespaceOfPrefix;
        private AttributeParameter[] _attributes;
        public XmlParameter() { }
        public XmlParameter(string name, params AttributeParameter[] attParas) : this(name, null, null, attParas) { }
        public XmlParameter(string name, string innerText, params AttributeParameter[] attParas) : this(name, innerText, null, attParas) { }
        public XmlParameter(string name, string innerText, string namespaceOfPrefix, params AttributeParameter[] attParas)
        {
            this._name = name;
            this._innerText = innerText;
            this._namespaceOfPrefix = namespaceOfPrefix;
            this._attributes = attParas;
        }
        ///
        /// 節點名稱
        ///
        public string Name
        {
            get { return this._name; }
            set { this._name = value; }
        }
        ///
        /// 節點文本
        ///
        public string InnerText
        {
            get { return this._innerText; }
            set { this._innerText = value; }
        }
        ///
        /// 節點前綴xmlns聲明(命名空間URI)
        ///
        public string NamespaceOfPrefix
        {
            get { return this._namespaceOfPrefix; }
            set { this._namespaceOfPrefix = value; }
        }
        ///
        /// 節點屬性集
        ///
        public AttributeParameter[] Attributes
        {
            get { return this._attributes; }
            set { this._attributes = value; }
        }
    }
    ///
    /// 節點屬性參數
    ///
    public class AttributeParameter
    {
        private string _name;
        private string _value;
        public AttributeParameter() { }
        public AttributeParameter(string attributeName, string attributeValue)
        {
            this._name = attributeName;
            this._value = attributeValue;
        }
        ///
        /// 屬性名稱
        ///
        public string Name
        {
            get { return this._name; }
            set { this._name = value; }
        }
        ///
        /// 屬性值
        ///
        public string Value
        {
            get { return this._value; }
            set { this._value = value; }
        }
    }
    ///
    /// FileName: XMLHelper
    /// Author: ziyou
    /// Create Date: 2011-5-11
    /// Email: [email protected]
    /// Version: 2.0.0.0
    /// Rewrite: 2011-5-11
    ///
    public class XMLHelper
    {
        private static string _xPath;
        ///
        /// xml文件路徑
        ///
        public static string XmlPath
        {
            get { return _xPath; }
            set { _xPath = value; }
        }
        private static string _configName = "XmlPath";
        ///
        /// 配置文件節點名稱,請設置在AppSettings節點下
        ///
        public static string ConfigName
        {
            get { return _configName; }
            set { _configName = value; GetConfig(); }
        }
        ///
        /// 從配置文件讀取xml路徑
        ///
        static void GetConfig()
        {
            if (string.IsNullOrEmpty(_xPath))
            {
                try
                {
                    _xPath = ConfigurationManager.AppSettings[_configName];
                }
                catch { }
            }
        }
        static XMLHelper() { GetConfig(); }
        #region private A
        ///
        /// 添加一個子節點
        ///
        /// XmlDocument對象
        /// 父節點
        /// Xml參數
        private static void A(XmlDocument xDoc, XmlNode parentNode, params XmlParameter[] xlParameter)
        {
            foreach (XmlParameter xpar in xlParameter)
            {
                XmlNode newNode = xDoc.CreateNode(XmlNodeType.Element, xpar.Name, null);
                string ns = string.IsNullOrEmpty(xpar.NamespaceOfPrefix) ? "" : newNode.GetNamespaceOfPrefix(xpar.NamespaceOfPrefix);
                foreach (AttributeParameter attp in xpar.Attributes)
                {
                    XmlNode attr = xDoc.CreateNode(XmlNodeType.Attribute, attp.Name, ns == "" ? null : ns);
                    attr.Value = attp.Value;
                    newNode.Attributes.SetNamedItem(attr);
                }
                newNode.InnerText = xpar.InnerText;
                parentNode.A(newNode);
            }
        }
        #endregion
        #region private AddEveryNode
        private static void AddEveryNode(XmlDocument xDoc, XmlNode parentNode, params XmlParameter[] paras)
        {
            XmlNodeList nlst = xDoc.DocumentElement.ChildNodes;
            foreach (XmlNode xns in nlst)
            {
                if (xns.Name == parentNode.Name)
                {
                    A(xDoc, xns, paras);
                }
                else
                {
                    foreach (XmlNode xn in xns)
                    {
                        if (xn.Name == parentNode.Name)
                        {
                            A(xDoc, xn, paras);
                        }
                    }
                }
            }
        }
        #endregion
        #region private GetXmlDom
        ///
        /// 獲得一個XmlDocument對象
        ///
        ///
        private static XmlDocument GetXmlDom()
        {
            XmlDocument xdoc = new XmlDocument();
            xdoc.Load(_xPath);
            return xdoc;
        }
        #endregion
        #region CreateXmlFile
        ///
        /// 創建一個XML文檔,成功創建後操作路徑將直接指向該文件
        ///
        /// 文件物理路徑名
        /// 根結點名稱
        /// 元素節點名稱
        /// XML參數
        public static void CreateXmlFile(string fileName, string rootNode, string elementName, params XmlParameter[] xmlParameter)
        {
            XmlDocument xDoc = new XmlDocument();
            XmlNode xn = xDoc.CreateXmlDeclaration("1.0", "UTF-8", null);
            xDoc.A(xn);
            XmlNode root = xDoc.createElement_x_x(rootNode);
            xDoc.A(root);
            XmlNode ln = xDoc.CreateNode(XmlNodeType.Element, elementName, null);
            A(xDoc, ln, xmlParameter);
            root.A(ln);
            try
            {
                xDoc.Save(fileName);
                _xPath = fileName;
            }
            catch
            {
                throw;
            }
            finally
            {
                xn = null;
                root = null;
                ln = null;
                xDoc = null;
                GC.Collect();
            }
        }
        ///
        /// 創建一個XML文檔,成功創建後操作路徑將直接指向該文件
        ///
        /// 文件物理路徑名
        /// xml字符串
        public static void CreateXmlFile(string fileName, string xmlString)
        {
            XmlDocument xDoc = new XmlDocument();
            try
            {
                xDoc.LoadXml(xmlString);
                xDoc.Save(fileName);
                _xPath = fileName;
            }
            catch { throw; }
            finally
            {
                xDoc = null;
                GC.Collect();
            }
        }
        #endregion
        #region AddNewNode
        ///
        /// 添加新節點
        ///
        /// 新節點的父節點對象
        /// Xml參數對象
        public static void AddNewNode(XmlNode parentNode, params XmlParameter[] xmlParameter)
        {
            XmlDocument xDoc = GetXmlDom();
            if (parentNode.Name == xDoc.DocumentElement.Name)
            {
                XmlNode newNode = xDoc.CreateNode(XmlNodeType.Element, xDoc.DocumentElement.ChildNodes[0].Name, null);
                A(xDoc, newNode, xmlParameter);
                xDoc.DocumentElement.A(newNode);
            }
            else
            {
                AddEveryNode(xDoc, parentNode, xmlParameter);
            }
            xDoc.Save(_xPath);
        }
        ///
        /// 添加新節點
        ///
        /// XmlDocument對象
        /// 新節點的父節點名稱
        /// XML參數對象
        public static void AddNewNode(string parentName, params XmlParameter[] xmlParameter)
        {
            XmlDocument xDoc = GetXmlDom();
            XmlNode parentNode = GetNode(xDoc, parentName);
            if (parentNode == null) return;
            if (parentNode.Name == xDoc.DocumentElement.Name)
            {
                XmlNode newNode = xDoc.CreateNode(XmlNodeType.Element, xDoc.DocumentElement.ChildNodes[0].Name, null);
                A(xDoc, newNode, xmlParameter);
                xDoc.DocumentElement.A(newNode);
            }
            else
            {
                AddEveryNode(xDoc, parentNode, xmlParameter);
            }
            xDoc.Save(_xPath);
        }
        #endregion
        #region AddAttribute
        ///
        /// 添加節點屬性
        ///
        /// 節點對象
        /// 該節點的命名空間URI
        /// 新屬性名稱
        /// 屬性值
        public static void AddAttribute(XmlNode node, string namespaceOfPrefix, string attributeName, string attributeValue)
        {
            XmlDocument xDoc = GetXmlDom();
            string ns = string.IsNullOrEmpty(namespaceOfPrefix) ? "" : node.GetNamespaceOfPrefix(namespaceOfPrefix);
            XmlNode xn = xDoc.CreateNode(XmlNodeType.Attribute, attributeName, ns == "" ? null : ns);
            xn.Value = attributeValue;
            node.Attributes.SetNamedItem(xn);
            xDoc.Save(_xPath);
        }
        ///
        /// 添加節點屬性
        ///
        /// 節點對象
        /// 該節點的命名空間URI
        /// 節點屬性參數
        public static void AddAttribute(XmlNode node, string namespaceOfPrefix, params AttributeParameter[] attributeParameters)
        {
            XmlDocument xDoc = GetXmlDom();
            string ns = string.IsNullOrEmpty(namespaceOfPrefix) ? "" : node.GetNamespaceOfPrefix(namespaceOfPrefix);
            foreach (AttributeParameter attp in attributeParameters)
            {
                XmlNode xn = xDoc.CreateNode(XmlNodeType.Attribute, attp.Name, ns == "" ? null : ns);
                xn.Value = attp.Value;
                node.Attributes.SetNamedItem(xn);
            }
            xDoc.Save(_xPath);
        }
        ///
        /// 添加節點屬性
        ///
        /// 節點名稱
        /// 該節點的命名空間URI
        /// 新屬性名稱
        /// 屬性值
        public static void AddAttribute(string nodeName, string namespaceOfPrefix, string attributeName, string attributeValue)
        {
            XmlDocument xDoc = GetXmlDom();
            XmlNodeList xlst = xDoc.DocumentElement.ChildNodes;
            for (int i = 0; i < xlst.Count; i++)
            {
                XmlNode node = GetNode(xlst[i], nodeName);
                if (node == null) break;
                AddAttribute(node, namespaceOfPrefix, attributeName, attributeValue);
            }
            xDoc.Save(_xPath);
        }
        ///
        /// 添加節點屬性
        ///
        /// 節點名稱
        /// 該節點的命名空間URI
        /// 節點屬性參數
        public static void AddAttribute(string nodeName, string namespaceOfPrefix, params AttributeParameter[] attributeParameters)
        {
            XmlDocument xDoc = GetXmlDom();
            XmlNodeList xlst = xDoc.DocumentElement.ChildNodes;
            for (int i = 0; i < xlst.Count; i++)
            {
                XmlNode node = GetNode(xlst[i], nodeName);
                if (node == null) break;
                AddAttribute(node, namespaceOfPrefix, attributeParameters);
            }
            xDoc.Save(_xPath);
        }
        #endregion
        #region GetNode
        ///
        /// 獲取指定節點名稱的節點對象
        ///
        /// 節點名稱
        ///
        public static XmlNode GetNode(string nodeName)
        {
            XmlDocument xDoc =GetXmlDom();
            if (xDoc.DocumentElement.Name == nodeName) return (XmlNode)xDoc.DocumentElement;
            XmlNodeList nlst = xDoc.DocumentElement.ChildNodes;
            foreach (XmlNode xns in nlst)  // 遍歷所有子節點
            {
                if (xns.Name == nodeName) return xns;
                else
                {
                    XmlNode xn = GetNode(xns, nodeName);
                    if (xn != null) return xn;  /// V1.0.0.3添加節點判斷
                }
            }
            return null;
        }
        ///
        /// 獲取指定節點名稱的節點對象
        ///
        /// 節點對象
        /// 節點名稱
        ///
        public static XmlNode GetNode(XmlNode node, string nodeName)
        {
            foreach (XmlNode xn in node)
            {
                if (xn.Name == nodeName) return xn;
                else
                {
                    XmlNode tmp = GetNode(xn, nodeName);
                    if (tmp != null) return tmp;
                }
            }
            return null;
        }
        ///
        /// 獲取指定節點名稱的節點對象
        ///
        /// 節點索引
        /// 節點名稱
        public static XmlNode GetNode(int index, string nodeName)
        {
            XmlDocument xDoc = GetXmlDom();
            XmlNodeList nlst = xDoc.DocumentElement.ChildNodes;
            if (nlst.Count <= index) return null;
            if (nlst[index].Name == nodeName) return (XmlNode)nlst[index];
            foreach (XmlNode xn in nlst[index])
            {
                return GetNode(xn, nodeName);
            }
            return null;
        }
        ///
        /// 獲取指定節點名稱的節點對象
        ///
        /// 節點對象
        /// 節點名稱
        /// 節點內容
        public static XmlNode GetNode(XmlNode node, string nodeName, string innerText)
        {
            foreach (XmlNode xn in node)
            {
                if (xn.Name == nodeName && xn.InnerText == innerText) return xn;
                else
                {
                    XmlNode tmp = GetNode(xn, nodeName, innerText);
                    if (tmp != null) return tmp;
                }
            }
            return null;
        }
        ///
        /// 獲取指定節點名稱的節點對象
        ///
        /// 節點名稱
        /// 節點內容
        public static XmlNode GetNode(string nodeName, string innerText)
        {
            XmlDocument xDoc = GetXmlDom();
            XmlNodeList nlst = xDoc.DocumentElement.ChildNodes;
            foreach (XmlNode xns in nlst)  // 遍歷所有子節點
            {
                if (xns.Name == nodeName && xns.InnerText == innerText) return xns;
                XmlNode tmp = GetNode(xns, nodeName, innerText);
                if (tmp != null) return tmp;
            }
            return null;
        }
        ///
        /// 獲取指定節點名稱的節點對象
        ///
        /// XML參數
        public static XmlNode GetNode(XmlParameter xmlParameter)
        {
            return GetNode(xmlParameter.Name, xmlParameter.InnerText);
        }
        ///
        /// 獲取指定節點名稱的節點對象
        ///
        /// 節點對象
        /// XML參數
        public static XmlNode GetNode(XmlNode node, XmlParameter xmlParameter)
        {
            return GetNode(node, xmlParameter.Name, node.InnerText);
        }
        #endregion
        #region UpdateNode
        private static void UpdateNode(XmlNode node, XmlParameter xmlParameter)
        {
            node.InnerText = xmlParameter.InnerText;
            for (int i = 0; i < xmlParameter.Attributes.Length; i++)
            {
                for (int j = 0; j < node.Attributes.Count; j++)
                {
                    if (node.Attributes[j].Name == xmlParameter.Attributes[i].Name)
                    {
                        node.Attributes[j].Value = xmlParameter.Attributes[i].Value;
                    }
                }
            }
        }
        private static void UpdateNode(XmlNode node, string innerText, AttributeParameter[] attributeParameters)
        {
            node.InnerText = innerText;
            for (int i = 0; i < attributeParameters.Length; i++)
            {
                for (int j = 0; j < node.Attributes.Count; j++)
                {
                    if (node.Attributes[j].Name == attributeParameters[i].Name)
                    {
                        node.Attributes[j].Value = attributeParameters[i].Value;
                    }
                }
            }
        }
        ///
        /// 修改節點的內容
        ///
        /// 節點索引
        /// XML參數對象
        public static void UpdateNode(int index, XmlParameter xmlParameter)
        {
            XmlDocument xDoc = GetXmlDom();
            XmlNodeList nlst = xDoc.DocumentElement.ChildNodes;
            if (nlst.Count <= index) return;
            if (nlst[index].Name == xmlParameter.Name)
            {
                UpdateNode(nlst[index], xmlParameter);
            }
            else
            {
                foreach (XmlNode xn in nlst[index])
                {
                    XmlNode xnd = GetNode(xn, xmlParameter.Name);
                    if (xnd != null)
                    {
                        UpdateNode(xnd, xmlParameter);
                    }
                }
            }
            xDoc.Save(_xPath);
        }
        ///
        /// 修改節點的內容
        ///
        /// 節點索引
        /// 節點名稱
        /// 修改後的內容
        public static void UpdateNode(int index, string nodeName, string newInnerText)
        {
            XmlDocument xDoc = GetXmlDom();
            XmlNodeList nlst = xDoc.DocumentElement.ChildNodes;
            if (nlst.Count <= index) return;
            if (nlst[index].Name == nodeName)
            {
                nlst[index].InnerText = newInnerText;
            }
            else
            {
                foreach (XmlNode xn in nlst[index])
                {
                    XmlNode xnd = GetNode(xn, nodeName);
                    if (xnd != null)
                    {
                        xnd.InnerText = newInnerText;
                    }
                }
            }
            xDoc.Save(_xPath);
        }
        ///
        /// 修改節點的內容
        ///
        /// XmlParameter對象
        /// 修改後的內容
        /// 需要修改的屬性
        public static void UpdateNode(XmlParameter xmlParameter, string innerText, params AttributeParameter[] attributeParameters)
        {
            XmlDocument xDoc = GetXmlDom();
            XmlNodeList nlst = xDoc.DocumentElement.ChildNodes;
            foreach (XmlNode xns in nlst)  // 遍歷所有子節點
            {
                if (xns.Name == xmlParameter.Name && xns.InnerText == xmlParameter.InnerText)
                {
                    UpdateNode(xns, innerText, attributeParameters);
                    break;
                }
                XmlNode tmp = GetNode(xns, xmlParameter);
                if (tmp != null)
                {
                    UpdateNode(tmp, innerText, attributeParameters);
                    break;
                }
            }
            xDoc.Save(_xPath);
        }
        #endregion
        #region DeleteNode
        ///
        /// 刪除節點
        ///
        /// 節點索引
        public static void DeleteNode(int index)
        {
            XmlDocument xDoc = GetXmlDom();
            XmlNodeList nlst = xDoc.DocumentElement.ChildNodes;
            nlst[index].ParentNode.RemoveChild(nlst[index]);
            xDoc.Save(_xPath);
        }
        ///
        /// 刪除節點
        ///
        /// 需要刪除的節點對象
        public static void DeleteNode(params XmlNode[] nodeList)
        {
            XmlDocument xDoc = GetXmlDom();
            foreach (XmlNode xnl in nodeList)
            {
                foreach (XmlNode xn in xDoc.DocumentElement.ChildNodes)
                {
                    if (xnl.Equals(xn))
                    {
                        xn.ParentNode.RemoveChild(xn);
                        break;
                    }
                }
            }
            xDoc.Save(_xPath);
        }
        ///
        /// 刪除節點
        ///
        /// XmlDocument對象
        /// 節點名稱
        /// 節點內容
        public static void DeleteNode(string nodeName, string nodeText)
        {
            XmlDocument xDoc = GetXmlDom();
            foreach (XmlNode xn in xDoc.DocumentElement.ChildNodes)
            {
                if (xn.Name == nodeName)
                {
                    if (xn.InnerText == nodeText)
                    {
                        xn.ParentNode.RemoveChild(xn);
                        return;
                    }
                }
                else
                {
                    XmlNode node = GetNode(xn, nodeName);
                    if (node != null && node.InnerText == nodeText)
                    {
                        node.ParentNode.ParentNode.RemoveChild(node.ParentNode);
                        return;
                    }
                }
            }
            xDoc.Save(_xPath);
        }
        #endregion
        #region SetAttribute
        ///
        /// 修改屬性值
        ///
        /// 元素對象
        /// 屬性參數
        private static void SetAttribute(XmlElement elem, params AttributeParameter[] attps)
        {
            foreach (AttributeParameter attp in attps)
            {
                elem.SetAttribute(attp.Name, attp.Value);
            }
        }
        ///
        /// 修改屬性值
        ///
        /// XML參數
        /// 屬性參數
        public static void SetAttribute(XmlParameter xmlParameter, params AttributeParameter[] attributeParameters)
        {
            XmlDocument xDoc = GetXmlDom();
            XmlNodeList nlst = xDoc.DocumentElement.ChildNodes;
            foreach (XmlNode xns in nlst)  // 遍歷所有子節點
            {
                if (xns.Name == xmlParameter.Name && xns.InnerText == xmlParameter.InnerText)
                {
                    SetAttribute((XmlElement)xns, attributeParameters);
                    break;
                }
                XmlNode tmp = GetNode(xns, xmlParameter);
                if (tmp != null)
                {
                    SetAttribute((XmlElement)tmp, attributeParameters);
                    break;
                }
            }
            xDoc.Save(_xPath);
        }
        ///
        /// 修改屬性值
        ///
        /// XML參數
        /// 新屬性值
        public static void SetAttribute(XmlParameter xmlParameter, string attributeName, string attributeValue)
        {
            XmlDocument xDoc = GetXmlDom();
            XmlNodeList nlst = xDoc.DocumentElement.ChildNodes;
            foreach (XmlNode xns in nlst)  // 遍歷所有子節點
            {
                if (xns.Name == xmlParameter.Name && xns.InnerText == xmlParameter.InnerText)
                {
                    ((XmlElement)xns).SetAttribute(attributeName, attributeValue);
                    break;
                }
                XmlNode tmp = GetNode(xns, xmlParameter);
                if (tmp != null)
                {
                    ((XmlElement)tmp).SetAttribute(attributeName, attributeValue);
                    break;
                }
            }
            xDoc.Save(_xPath);
        }
        #endregion
    }

}



http://yunpan.cn/Q7czcYTwE8qkc  提取碼 545c

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章