Realm_King 之 .NET操作XML完整类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Web;
namespace BLL
{
??? public? class XmlDoc
??? {
??????? /// <summary>??
??????? /// 創(chuàng)建Xml文件??
??????? /// </summary>??
??????? /// <param name="xmlPath">創(chuàng)建文件路徑</param>??
??????? /// <param name="element"></param>??
??????? public void CreateXmlNode(string xmlPath, string element)
??????? {
??????????? //實(shí)例化Xml文檔類(lèi)??
??????????? XmlTextWriter xmlTextWriter = new XmlTextWriter(xmlPath, Encoding.UTF8);
??????????? //創(chuàng)建Xml聲明??
??????????? xmlTextWriter.WriteStartDocument();
??????????? xmlTextWriter.WriteStartElement(element);
??????????? xmlTextWriter.Flush();
??????????? xmlTextWriter.Close();
??????? }
??????? /// <summary>??
??????? /// 新增Xml節(jié)點(diǎn)
??????? /// </summary>??
??????? /// <param name="xmlPath">文件路徑</param>??
??????? /// <param name="singleNode"></param>
??????? /// <param name="node"></param>??
??????? public void AddXmlElement(string xmlPath, string singleNode, XmlNode node)
??????? {
??????????? //實(shí)例化Xml文檔類(lèi)??
??????????? XmlDocument xmlDocument = new XmlDocument();
??????????? //加載Xml文件??
??????????? xmlDocument.Load(xmlPath);
??????????? xmlDocument.SelectSingleNode(singleNode).AppendChild(node);
??????????? xmlDocument.Save(xmlPath);
??????? }
??????? /// <summary>??
??????? /// 新增Xml節(jié)點(diǎn)??
??????? /// </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)
??????? {
??????????? //實(shí)例化Xml文檔類(lèi)??
??????????? 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
??????????? {
??????????????? //實(shí)例化Xml文檔類(lèi)??
??????????????? 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表達(dá)式的第一個(gè)XmlNode</param>??
??????? /// <returns></returns>??
??????? public XmlNode GetXmlNode(string xmlPath, string singleNode)
??????? {
??????????? try
??????????? {
??????????????? //實(shí)例化Xml文檔類(lèi)??
??????????????? XmlDocument xmlDocument = new XmlDocument();
??????????????? //加載Xml文件??
??????????????? xmlDocument.Load(GetMapPath(xmlPath));
??????????????? //獲取文件內(nèi)容??
??????????????? 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表達(dá)式的第一個(gè)XmlNode</param>??
??????? /// <param name="xmlNodeString">節(jié)點(diǎn)名稱(chēng)</param>??
??????? /// <returns></returns>??
??????? public XmlElement GetXmlElement(string xmlPath, string singleNode, string xmlNodeString)
??????? {
??????????? try
??????????? {
??????????????? //實(shí)例化Xml文檔類(lèi)??
??????????????? XmlDocument xmlDocument = new XmlDocument();
??????????????? //加載Xml文件??
??????????????? xmlDocument.Load(GetMapPath(xmlPath));
??????????????? //獲取文件內(nèi)容??
??????????????? XmlNode xmlNode = xmlDocument.SelectSingleNode(singleNode);
??????????????? //提取節(jié)點(diǎn)名稱(chēng)下的屬性值??
??????????????? 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文件的節(jié)點(diǎn)屬性??
??????? /// </summary>??
??????? /// <param name="xmlPath">Xml文件路徑</param>??
??????? /// <param name="singleNode">選擇匹配XPath表達(dá)式的第一個(gè)XmlNode</param>??
??????? /// <param name="xmlNodeString">節(jié)點(diǎn)名稱(chēng)</param>??
??????? /// <param name="variables">節(jié)點(diǎn)屬性</param>??
??????? /// <returns></returns>??
??????? public string GetAttribute(string xmlPath, string singleNode, string xmlNodeString, string variables)
??????? {
??????????? try
??????????? {
??????????????? //實(shí)例化Xml文檔類(lèi)??
??????????????? XmlDocument xmlDocument = new XmlDocument();
??????????????? //加載Xml文件??
??????????????? xmlDocument.Load(GetMapPath(xmlPath));
??????????????? //獲取文件內(nèi)容??
??????????????? XmlNode xmlNode = xmlDocument.SelectSingleNode(singleNode);
??????????????? //提取節(jié)點(diǎn)名稱(chēng)下的屬性值??
??????????????? 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>??
??????? /// 設(shè)置XML節(jié)點(diǎn)??
??????? /// </summary>??
??????? /// <param name="xmlNodeString">節(jié)點(diǎn)名稱(chēng)</param>??
??????? /// <param name="value">節(jié)點(diǎn)值</param>??
??????? /// <param name="discrption">節(jié)點(diǎn)說(shuō)明</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));
??????????????? //獲取文件內(nèi)容??
??????????????? XmlNode xmlNode = xmlDocument.SelectSingleNode(singleNode);
??????????????? //設(shè)置指定XML節(jié)點(diǎn)Value值??
??????????????? xmlNode[xmlNodeString].SetAttribute(attribute, value);
??????????????? xmlNode[xmlNodeString].SetAttribute("Description", description);
??????????????? //將設(shè)置后的XML節(jié)點(diǎn)保存??
??????????????? xmlDocument.Save(GetMapPath(xmlPath));
??????????? }
??????????? catch (Exception exp)
??????????? {
??????????????? throw new Exception(exp.Message);
??????????? }
??????? }
??????? /// <summary>??
??????? /// 設(shè)置XML節(jié)點(diǎn)??
??????? /// </summary>??
??????? /// <param name="xmlPath">XML文件路徑</param>??
??????? /// <param name="singleNode">選擇匹配XPath表達(dá)式的第一個(gè)XmlNode</param>??
??????? /// <param name="xmlNodeString">指定節(jié)點(diǎn)</param>??
??????? /// <param name="attribute">屬性數(shù)組</param>??
??????? /// <param name="value">值數(shù)組</param>??
??????? public void SetAttribute(string xmlPath, string singleNode, string xmlNodeString, string[] attribute, string[] value)
??????? {
??????????? try
??????????? {
??????????????? XmlDocument xmlDocument = new XmlDocument();
??????????????? xmlDocument.Load(GetMapPath(xmlPath));
??????????????? //獲取文件內(nèi)容??
??????????????? XmlNode xmlNode = xmlDocument.SelectSingleNode(singleNode);
??????????????? for (int i = 0; i < attribute.Length; i++)
??????????????? {
??????????????????? //設(shè)置指定XML節(jié)點(diǎn)Value值??
??????????????????? xmlNode[xmlNodeString].SetAttribute(attribute[i], value[i]);
??????????????? }
??????????????? //將設(shè)置后的XML節(jié)點(diǎn)保存??
??????????????? xmlDocument.Save(xmlPath);
??????????? }
??????????? catch (Exception exp)
??????????? {
??????????????? throw new Exception(exp.Message);
??????????? }
??????? }
??????? /// <summary>??
??????? /// 通過(guò)XML名稱(chēng)獲取XML物理路徑,可無(wú)限添加,前提是XML都位于同一網(wǎng)站目錄下??
??????? /// </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;
??????? }
??? }
}
?
好了不多說(shuō)了,如果有bug出現(xiàn)希望大家多多指點(diǎn)....
轉(zhuǎn)載于:https://www.cnblogs.com/RealmKing/p/3414329.html
總結(jié)
以上是生活随笔為你收集整理的Realm_King 之 .NET操作XML完整类的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 大家说我长得像周海媚吗?[已扎口]
- 下一篇: 哪个奥义好