XMLOperator[C#]

zhang98722發表於2011-09-15
public class XMLOperator
    {
        //宣告一個XmlDocument空物件
        protected XmlDocument XmlDoc = new XmlDocument();

        /// <summary>
        /// 建構函式,匯入Xml檔案
        /// </summary>
        /// <param name="xmlFile">檔案虛擬路徑</param>
        public XMLOperator(string xmlFile)
        {
            try
            {
                XmlDoc.Load(xmlFile); //載入Xml文件
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 解構函式
        /// </summary>
        ~XMLOperator()
        {
            XmlDoc = null; //釋放XmlDocument物件
        }

        /// <summary>
        /// 儲存檔案
        /// </summary>
        /// <param name="filePath">檔案虛擬路徑</param>
        public void Save(string filePath)
        {
            try
            {
                XmlDoc.Save(filePath);
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 返回Xml檔案實際路徑
        /// </summary>
        /// <param name="xmlFile">檔案虛擬路徑</param>
        /// <returns></returns>
        public string GetXmlFilePath(string xmlFile)
        {

            return HttpContext.Current.Server.MapPath(xmlFile);
        }

        /// <summary>
        /// 屬性查詢,返回屬性值
        /// </summary>
        /// <param name="XmlPathNode">屬性所在的節點</param>
        /// <param name="Attrib">屬性</param>
        /// <returns></returns>
        public string SelectAttrib(string XmlPathNode, string Attrib)
        {
            string _strNode = "";
            try
            {
                _strNode = XmlDoc.SelectSingleNode(XmlPathNode).Attributes[Attrib].Value;
            }
            catch
            { }
            return _strNode;
        }

        /// <summary>
        /// 節點查詢,返回節點值
        /// </summary>
        /// <param name="XmlPathNode">節點的路徑</param>
        /// <returns></returns>
        public string SelectNodeText(string XmlPathNode)
        {
            string _nodeTxt = XmlDoc.SelectSingleNode(XmlPathNode).InnerText;
            if (_nodeTxt == null || _nodeTxt == "")
                return "";
            else
                return _nodeTxt;
        }

        /// <summary>
        /// 節點值查詢判斷
        /// </summary>
        /// <param name="XmlPathNode">父節點</param>
        /// <param name="index">節點索引</param>
        /// <param name="NodeText">節點值</param>
        /// <returns></returns>
        public bool SelectNode(string XmlPathNode, int index, string NodeText)
        {
            try
            {
                XmlNodeList _NodeList = XmlDoc.SelectNodes(XmlPathNode);
                //迴圈遍歷節點,查詢是否存在該節點
                for (int i = 0; i < _NodeList.Count; i++)
                {
                    if (_NodeList[i].ChildNodes[index].InnerText == NodeText)
                    {
                        return true;
                    }
                }
            }
            catch
            {
            }
            return false;
        }

        /// <summary>
        /// 獲取子節點個數
        /// </summary>
        /// <param name="XmlPathNode">父節點</param>
        /// <returns></returns>
        public int NodeCount(string XmlPathNode)
        {
            int i = 0;
            try
            {
                i = XmlDoc.SelectSingleNode(XmlPathNode).ChildNodes.Count;
            }
            catch
            {
                i = 0;
            }
            return i;
        }

        /// <summary>
        /// 更新一個節點的內容
        /// </summary>
        /// <param name="XmlPathNode">節點的路徑</param>
        /// <param name="Content">新的節點值</param>
        /// <returns></returns>
        public bool UpdateNode(string XmlPathNode, string NodeContent)
        {
            try
            {
                XmlDoc.SelectSingleNode(XmlPathNode).InnerText = NodeContent;
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 更新N個節點值
        /// </summary>
        /// <param name="XmlParentNode">父節點</param>
        /// <param name="XmlNode">子節點</param>
        /// <param name="NodeContent">子節點內容</param>
        /// <returns></returns>
        public bool UpdateNode(string XmlParentNode, string[] XmlNode, string[] NodeContent)
        {
            try
            {
                //根據節點陣列迴圈修改節點值
                for (int i = 0; i < XmlNode.Length; i++)
                {
                    XmlDoc.SelectSingleNode(XmlParentNode + "/" + XmlNode[i]).InnerText = NodeContent[i];
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 修改屬性
        /// </summary>
        /// <param name="XmlPathNode">屬性所在的節點</param>
        /// <param name="Attrib">屬性名</param>
        /// <param name="Content">屬性值</param>
        /// <returns></returns>
        public bool UpdateAttrib(string XmlPathNode, string Attrib, string AttribContent)
        {
            try
            {
                //修改屬性值
                XmlDoc.SelectSingleNode(XmlPathNode).Attributes[Attrib].Value = AttribContent;
                return true;
            }
            catch
            {
                return false;
            }
        }


        /// <summary>
        /// 新增屬性
        /// </summary>
        /// <param name="MainNode">屬性所在節點</param>
        /// <param name="Attrib">屬性名</param>
        /// <param name="AttribContent">屬性值</param>
        /// <returns></returns>
        public bool InsertAttrib(string MainNode, string Attrib, string AttribContent)
        {
            try
            {
                XmlElement objNode = (XmlElement)XmlDoc.SelectSingleNode(MainNode); //強制轉化成XmlElement物件
                objNode.SetAttribute(Attrib, AttribContent); //XmlElement物件新增屬性方法   
                return true;
            }
            catch
            {
                return false;
            }
        }


        /// <summary>
        /// 插入一個節點,帶N個子節點
        /// </summary>
        /// <param name="MainNode">插入節點的父節點</param>
        /// <param name="ChildNode">插入節點的元素名</param>
        /// <param name="Element">插入節點的子節點名陣列</param>
        /// <param name="Content">插入節點的子節點內容陣列</param>
        /// <returns></returns>
        public bool InsertNode(string MainNode, string ChildNode, string[] Element, string[] Content)
        {
            try
            {
                XmlNode objRootNode = XmlDoc.SelectSingleNode(MainNode); //宣告XmlNode物件
                XmlElement objChildNode = XmlDoc.CreateElement(ChildNode); //建立XmlElement物件
                objRootNode.AppendChild(objChildNode);
                for (int i = 0; i < Element.Length; i++) //迴圈插入節點元素
                {
                    XmlElement objElement = XmlDoc.CreateElement(Element[i]);
                    objElement.InnerText = Content[i];
                    objChildNode.AppendChild(objElement);
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 刪除一個節點
        /// </summary>
        /// <param name="Node">要刪除的節點</param>
        /// <returns></returns>
        public bool DeleteNode(string Node)
        {
            try
            {
                //XmlNode的RemoveChild方法來刪除節點及其所有子節點
                XmlDoc.SelectSingleNode(Node).ParentNode.RemoveChild(XmlDoc.SelectSingleNode(Node));
                return true;
            }
            catch
            {
                return false;
            }
        }
    }