日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

Realm_King 之 .NET操作XML完整类

發布時間:2023/11/27 生活经验 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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>??
??????? /// 創建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>??
??????? /// <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;
??????? }
??? }
}

?

好了不多說了,如果有bug出現希望大家多多指點....

轉載于:https://www.cnblogs.com/RealmKing/p/3414329.html

總結

以上是生活随笔為你收集整理的Realm_King 之 .NET操作XML完整类的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。