.NET操作XML完整類

來源:互聯網
上載者:User
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Xml;using System.Web;    namespace BLL{    public  class XmlDoc    {        /// <summary>          /// 建立Xml檔案          /// </summary>          /// <param name="xmlPath">建立檔案路徑</param>          /// <param name="element"></param>          public void CreateXmlNode(string xmlPath, string element)        {            //執行個體化Xml文檔類              XmlTextWriter xmlTextWriter = new XmlTextWriter(xmlPath, Encoding.UTF8);            //建立Xml聲明              xmlTextWriter.WriteStartDocument();            xmlTextWriter.WriteStartElement(element);            xmlTextWriter.Flush();            xmlTextWriter.Close();        }            /// <summary>          /// 新增Xml節點        /// </summary>          /// <param name="xmlPath">檔案路徑</param>          /// <param name="singleNode"></param>        /// <param name="node"></param>          public void AddXmlElement(string xmlPath, string singleNode, XmlNode node)        {            //執行個體化Xml文檔類              XmlDocument xmlDocument = new XmlDocument();            //載入Xml檔案              xmlDocument.Load(xmlPath);            xmlDocument.SelectSingleNode(singleNode).AppendChild(node);            xmlDocument.Save(xmlPath);        }            /// <summary>          /// 新增Xml節點          /// </summary>          /// <param name="xmlPath"></param>          /// <param name="singleNode"></param>          /// <param name="node"></param>          public void AddXmlElement(string xmlPath, string singleNode, string element, string attribute, string value)        {            //執行個體化Xml文檔類              XmlDocument xmlDocument = new XmlDocument();            //載入Xml檔案              xmlDocument.Load(xmlPath);            XmlNode xmlNode = xmlDocument.SelectSingleNode(singleNode);            XmlElement xmlElement = xmlDocument.CreateElement(element); ;            xmlElement.SetAttribute(attribute, value);            xmlDocument.Save(xmlPath);        }                /// <summary>          /// 讀取XML檔案的XmlDocument          /// </summary>          /// <param name="xmlPath">Xml檔案路徑</param>          /// <returns></returns>          public XmlDocument GetXmlDocument(string xmlPath)        {            try            {                //執行個體化Xml文檔類                  XmlDocument xmlDocument = new XmlDocument();                //載入Xml檔案                  xmlDocument.Load(GetMapPath(xmlPath));                return xmlDocument;            }            catch (System.Xml.XmlException exp)            {                throw exp;            }            catch (System.IO.IOException exp)            {                throw exp;            }            catch (ArgumentNullException exp)            {                throw exp;            }            catch (ArgumentException exp)            {                throw exp;            }            catch (Exception exp)            {                throw exp;            }        }            /// <summary>          /// 讀取XML檔案的XmlNode          /// </summary>          /// <param name="xmlPath">Xml檔案路徑</param>          /// <param name="singleNode">選擇匹配XPath運算式的第一個XmlNode</param>          /// <returns></returns>          public XmlNode GetXmlNode(string xmlPath, string singleNode)        {            try            {                //執行個體化Xml文檔類                  XmlDocument xmlDocument = new XmlDocument();                //載入Xml檔案                  xmlDocument.Load(GetMapPath(xmlPath));                //擷取檔案內容                  return xmlDocument.SelectSingleNode(singleNode);            }            catch (System.Xml.XPath.XPathException exp)            {                throw exp;            }            catch (System.Xml.XmlException exp)            {                throw exp;            }            catch (System.IO.IOException exp)            {                throw exp;            }            catch (ArgumentNullException exp)            {                throw exp;            }            catch (ArgumentException exp)            {                throw exp;            }            catch (Exception exp)            {                throw exp;            }        }            /// <summary>          /// 讀取XML檔案的XmlElement          /// </summary>          /// <param name="xmlPath">Xml檔案路徑</param>          /// <param name="singleNode">選擇匹配XPath運算式的第一個XmlNode</param>          /// <param name="xmlNodeString">節點名稱</param>          /// <returns></returns>          public XmlElement GetXmlElement(string xmlPath, string singleNode, string xmlNodeString)        {            try            {                //執行個體化Xml文檔類                  XmlDocument xmlDocument = new XmlDocument();                //載入Xml檔案                  xmlDocument.Load(GetMapPath(xmlPath));                //擷取檔案內容                  XmlNode xmlNode = xmlDocument.SelectSingleNode(singleNode);                //提取節點名稱下的屬性值                  return xmlNode[xmlNodeString];            }            catch (System.Xml.XPath.XPathException exp)            {                throw exp;            }            catch (System.Xml.XmlException exp)            {                throw exp;            }            catch (System.IO.IOException exp)            {                throw exp;            }            catch (ArgumentNullException exp)            {                throw exp;            }            catch (ArgumentException exp)            {                throw exp;            }            catch (Exception exp)            {                throw exp;            }            }            /// <summary>          /// 讀取XML檔案的節點屬性          /// </summary>          /// <param name="xmlPath">Xml檔案路徑</param>          /// <param name="singleNode">選擇匹配XPath運算式的第一個XmlNode</param>  ///查看本欄目更多精彩內容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/net/        /// <param name="xmlNodeString">節點名稱</param>          /// <param name="variables">節點屬性</param>          /// <returns></returns>          public string GetAttribute(string xmlPath, string singleNode, string xmlNodeString, string variables)        {            try            {                //執行個體化Xml文檔類                  XmlDocument xmlDocument = new XmlDocument();                //載入Xml檔案                  xmlDocument.Load(GetMapPath(xmlPath));                //擷取檔案內容                  XmlNode xmlNode = xmlDocument.SelectSingleNode(singleNode);                //提取節點名稱下的屬性值                  return xmlNode[xmlNodeString].GetAttribute(variables);            }            catch (System.Xml.XPath.XPathException exp)            {                throw exp;            }            catch (System.Xml.XmlException exp)            {                throw exp;            }            catch (System.IO.IOException exp)            {                throw exp;            }            catch (ArgumentNullException exp)            {                throw exp;            }            catch (ArgumentException exp)            {                throw exp;            }            catch (Exception exp)            {                throw exp;            }            }            /// <summary>          /// 設定XML節點          /// </summary>          /// <param name="xmlNodeString">節點名稱</param>          /// <param name="value">節點值</param>          /// <param name="discrption">節點說明</param>          public void SetAttribute(string xmlPath, string singleNode, string xmlNodeString, string attribute, string value, string description)        {            try            {                XmlDocument xmlDocument = new XmlDocument();                    xmlDocument.Load(GetMapPath(xmlPath));                //擷取檔案內容                  XmlNode xmlNode = xmlDocument.SelectSingleNode(singleNode);                //設定指定XML節點Value值                  xmlNode[xmlNodeString].SetAttribute(attribute, value);                xmlNode[xmlNodeString].SetAttribute("Description", description);                //將設定後的XML節點儲存                  xmlDocument.Save(GetMapPath(xmlPath));            }            catch (Exception exp)            {                throw new Exception(exp.Message);            }        }            /// <summary>          /// 設定XML節點          /// </summary>          /// <param name="xmlPath">XML檔案路徑</param>          /// <param name="singleNode">選擇匹配XPath運算式的第一個XmlNode</param>          /// <param name="xmlNodeString">指定節點</param>          /// <param name="attribute">屬性數組</param>          /// <param name="value">值數組</param>          public void SetAttribute(string xmlPath, string singleNode, string xmlNodeString, string[] attribute, string[] value)        {            try            {                XmlDocument xmlDocument = new XmlDocument();                    xmlDocument.Load(GetMapPath(xmlPath));                //擷取檔案內容                  XmlNode xmlNode = xmlDocument.SelectSingleNode(singleNode);                for (int i = 0; i < attribute.Length; i++)                {                    //設定指定XML節點Value值                      xmlNode[xmlNodeString].SetAttribute(attribute[i], value[i]);                }                //將設定後的XML節點儲存                  xmlDocument.Save(xmlPath);            }            catch (Exception exp)            {                throw new Exception(exp.Message);            }        }            /// <summary>          /// 通過XML名稱擷取XML實體路徑,可無限添加,前提是XML都位於同一網站目錄下          /// </summary>          /// <param name="xmlName"></param>          /// <returns></returns>          private string GetMapPath(string xmlName)        {            switch (xmlName)            {                case "GlobalVariables.xml":                    xmlName = HttpContext.Current.Server.MapPath("~/XML/") + xmlName;                    break;                default:                    break;            }            return xmlName;        }    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.