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; } }}