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> ///查看本欄目