您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關c#中xml操作類的方法,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.IO; using System.Xml; using System.Text; namespace MSCL { /// <summary> /// XML 操作基類 /// </summary> public class XmlHelper : IDisposable { //以下為單一功能的靜態類 #region 讀取XML到DataSet /**//************************************************** * 函數名稱:GetXml(string XmlPath) * 功能說明:讀取XML到DataSet * 參 數:XmlPath:xml文檔路徑 * 使用示列: * using EC; //引用命名空間 * string xmlPath = Server.MapPath("/EBDnsConfig/DnsConfig.xml"); //獲取xml路徑 * DataSet ds = EC.XmlObject.GetXml(xmlPath); //讀取xml到DataSet中 ************************************************/ /**//// <summary> /// 功能:讀取XML到DataSet中 /// </summary> /// <param name="XmlPath">xml路徑</param> /// <returns>DataSet</returns> public static DataSet GetXml(string XmlPath) { DataSet ds = new DataSet(); ds.ReadXml(@XmlPath); return ds; } #endregion #region 讀取xml文檔并返回一個節點 /**//************************************************** * 函數名稱:ReadXmlReturnNode(string XmlPath,string Node) * 功能說明:讀取xml文檔并返回一個節點:適用于一級節點 * 參 數: XmlPath:xml文檔路徑;Node 返回的節點名稱 * 適應用Xml:<?xml version="1.0" encoding="utf-8" ?> * <root> * <dns1>ns1.everdns.com</dns1> * </root> * 使用示列: * using EC; //引用命名空間 * string xmlPath = Server.MapPath("/EBDnsConfig/DnsConfig.xml"); //獲取xml路徑 * Response.Write(XmlObject.ReadXmlReturnNode(xmlPath, "mailmanager")); ************************************************/ /**//// <summary> /// 讀取xml文檔并返回一個節點:適用于一級節點 /// </summary> /// <param name="XmlPath">xml路徑</param> /// <param name="NodeName">節點</param> /// <returns></returns> public static string ReadXmlReturnNode(string XmlPath, string Node) { XmlDocument docXml = new XmlDocument(); docXml.Load(@XmlPath); XmlNodeList xn = docXml.GetElementsByTagName(Node); return xn.Item(0).InnerText.ToString(); } #endregion #region 查找數據,返回一個DataSet /**//************************************************** * 函數名稱:GetXmlData(string xmlPath, string XmlPathNode) * 功能說明:查找數據,返回當前節點的所有下級節點,填充到一個DataSet中 * 參 數:xmlPath:xml文檔路徑;XmlPathNode:當前節點的路徑 * 使用示列: * using EC; //引用命名空間 * string xmlPath = Server.MapPath("/EBDomainConfig/DomainConfig.xml"); //獲取xml路徑 * DataSet ds = new DataSet(); * ds = XmlObject.GetXmlData(xmlPath, "root/items");//讀取當前路徑 * this.GridView1.DataSource = ds; * this.GridView1.DataBind(); * ds.Clear(); * ds.Dispose(); * Xml示例: * <?xml version="1.0" encoding="utf-8" ?> * <root> * <items name="xinnet"> * <url>http://www.paycenter.com.cn/cgi-bin/</url> * <port>80</port> * </items> * </root> ************************************************/ /**//// <summary> /// 查找數據,返回當前節點的所有下級節點,填充到一個DataSet中 /// </summary> /// <param name="xmlPath">xml文檔路徑</param> /// <param name="XmlPathNode">節點的路徑:根節點/父節點/當前節點</param> /// <returns></returns> public static DataSet GetXmlData(string xmlPath, string XmlPathNode) { XmlDocument objXmlDoc = new XmlDocument(); objXmlDoc.Load(xmlPath); DataSet ds = new DataSet(); StringReader read = new StringReader(objXmlDoc.SelectSingleNode(XmlPathNode).OuterXml); ds.ReadXml(read); return ds; } #endregion #region 更新Xml節點內容 /**//************************************************** * 函數名稱:XmlNodeReplace(string xmlPath,string Node,string Content) * 功能說明:更新Xml節點內容 * 參 數:xmlPath:xml文檔路徑;Node:當前節點的路徑;Content:內容 * 使用示列: * using EC; //引用命名空間 * string xmlPath = Server.MapPath("/EBDomainConfig/DomainConfig.xml"); //獲取xml路徑 * XmlObject.XmlNodeReplace(xmlPath, "root/test", "56789"); //更新節點內容 ************************************************/ /**//// <summary> /// 更新Xml節點內容 /// </summary> /// <param name="xmlPath">xml路徑</param> /// <param name="Node">要更換內容的節點:節點路徑 根節點/父節點/當前節點</param> /// <param name="Content">新的內容</param> public static void XmlNodeReplace(string xmlPath, string Node, string Content) { XmlDocument objXmlDoc = new XmlDocument(); objXmlDoc.Load(xmlPath); objXmlDoc.SelectSingleNode(Node).InnerText = Content; objXmlDoc.Save(xmlPath); } #endregion #region 刪除XML節點和此節點下的子節點 /**//************************************************** * 函數名稱:XmlNodeDelete(string xmlPath,string Node) * 功能說明:刪除XML節點和此節點下的子節點 * 參 數:xmlPath:xml文檔路徑;Node:當前節點的路徑; * 使用示列: * using EC; //引用命名空間 * string xmlPath = Server.MapPath("/EBDomainConfig/DomainConfig.xml"); //獲取xml路徑 * XmlObject.XmlNodeDelete(xmlPath, "root/test"); //刪除當前節點 ************************************************/ /**//// <summary> /// 刪除XML節點和此節點下的子節點 /// </summary> /// <param name="xmlPath">xml文檔路徑</param> /// <param name="Node">節點路徑</param> public static void XmlNodeDelete(string xmlPath, string Node) { XmlDocument objXmlDoc = new XmlDocument(); objXmlDoc.Load(xmlPath); string mainNode = Node.Substring(0, Node.LastIndexOf("/")); objXmlDoc.SelectSingleNode(mainNode).RemoveChild(objXmlDoc.SelectSingleNode(Node)); objXmlDoc.Save(xmlPath); } #endregion #region 插入一個節點和此節點的子節點 /**//************************************************** * 函數名稱:XmlInsertNode(string xmlPath, string MailNode, string ChildNode, string Element,string Content) * 功能說明:插入一個節點和此節點的字節點 * 參 數:xmlPath:xml文檔路徑;MailNode:當前節點的路徑;ChildNode:新插入的節點;Element:插入節點的子節點;Content:子節點的內容 * 使用示列: * using EC; //引用命名空間 * string xmlPath = Server.MapPath("/EBDomainConfig/DomainConfig.xml"); //獲取xml路徑 * XmlObject.XmlInsertNode(xmlPath, "root/test","test1","test2","測試內容"); //插入一個節點和此節點的字節點 * 生成的XML格式為 * <test> * <test1> * <test2>測試內容</test2> * </test1> * </test> ************************************************/ /**//// <summary> /// 插入一個節點和此節點的子節點 /// </summary> /// <param name="xmlPath">xml路徑</param> /// <param name="MailNode">當前節點路徑</param> /// <param name="ChildNode">新插入節點</param> /// <param name="Element">插入節點的子節點</param> /// <param name="Content">子節點的內容,已經應用CDATA</param> public static void XmlInsertNode(string xmlPath, string MailNode, string ChildNode, string Element, string Content) { XmlDocument objXmlDoc = new XmlDocument(); objXmlDoc.Load(xmlPath); XmlNode objRootNode = objXmlDoc.SelectSingleNode(MailNode); XmlElement objChildNode = objXmlDoc.CreateElement(ChildNode); objRootNode.AppendChild(objChildNode); XmlElement objElement = objXmlDoc.CreateElement(Element); XmlCDataSection xcds = objXmlDoc.CreateCDataSection(Content); objElement.AppendChild(xcds); objChildNode.AppendChild(objElement); objXmlDoc.Save(xmlPath); } #endregion #region 插入一節點,帶一屬性 /**//************************************************** * 函數名稱:XmlInsertElement(string xmlPath, string MainNode, string Element, string Attrib, string AttribContent, string Content) * 功能說明:插入一節點,帶一屬性 * 參 數:xmlPath:xml文檔路徑;MailNode:當前節點的路徑;Element:新插入的節點;Attrib:屬性名稱;AttribContent:屬性值;Content:節點的內容 * 使用示列: * using EC; //引用命名空間 * string xmlPath = Server.MapPath("/EBDomainConfig/DomainConfig.xml"); //獲取xml路徑 * XmlObject.XmlInsertElement(xmlPath, "root/test", "test1", "Attrib", "屬性值", "節點內容"); //插入一節點,帶一屬性 * 生成的XML格式為 * <test> * <test1 Attrib="屬性值">節點內容</test1> * </test> ************************************************/ /**//// <summary> /// 插入一節點,帶一屬性 /// </summary> /// <param name="xmlPath">Xml文檔路徑</param> /// <param name="MainNode">當前節點路徑</param> /// <param name="Element">新節點</param> /// <param name="Attrib">屬性名稱</param> /// <param name="AttribContent">屬性值</param> /// <param name="Content">新節點值</param> public static void XmlInsertElement(string xmlPath, string MainNode, string Element, string Attrib, string AttribContent, string Content) { XmlDocument objXmlDoc = new XmlDocument(); objXmlDoc.Load(xmlPath); XmlNode objNode = objXmlDoc.SelectSingleNode(MainNode); XmlElement objElement = objXmlDoc.CreateElement(Element); objElement.SetAttribute(Attrib, AttribContent); objElement.InnerText = Content; objNode.AppendChild(objElement); objXmlDoc.Save(xmlPath); } #endregion #region 插入一節點不帶屬性 /**//************************************************** * 函數名稱:XmlInsertElement(string xmlPath, string MainNode, string Element, string Content) * 功能說明:插入一節點不帶屬性 * 參 數:xmlPath:xml文檔路徑;MailNode:當前節點的路徑;Element:新插入的節點;Content:節點的內容 * 使用示列: * using EC; //引用命名空間 * string xmlPath = Server.MapPath("/EBDomainConfig/DomainConfig.xml"); //獲取xml路徑 * XmlObject.XmlInsertElement(xmlPath, "root/test", "text1", "節點內容"); //插入一節點不帶屬性 * 生成的XML格式為 * <test> * <text1>節點內容</text1> * </test> ************************************************/ public static void XmlInsertElement(string xmlPath, string MainNode, string Element, string Content) { XmlDocument objXmlDoc = new XmlDocument(); objXmlDoc.Load(xmlPath); XmlNode objNode = objXmlDoc.SelectSingleNode(MainNode); XmlElement objElement = objXmlDoc.CreateElement(Element); objElement.InnerText = Content; objNode.AppendChild(objElement); objXmlDoc.Save(xmlPath); } #endregion //必須創建對象才能使用的類 private bool _alreadyDispose = false; private XmlDocument xmlDoc = new XmlDocument(); private XmlNode xmlNode; private XmlElement xmlElem; #region 構造與釋構 public XmlHelper() { } ~XmlHelper() { Dispose(); } protected virtual void Dispose(bool isDisposing) { if (_alreadyDispose) return; if (isDisposing) { // } _alreadyDispose = true; } #endregion #region IDisposable 成員 public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } #endregion #region 創建xml文檔 /**//************************************************** * 對象名稱:XmlObject * 功能說明:創建xml文檔 * 使用示列: * using EC; //引用命名空間 * string xmlPath = Server.MapPath("test.xml"); * XmlObject obj = new XmlObject(); * 創建根節點 * obj.CreateXmlRoot("root"); * // 創建空節點 * //obj.CreatXmlNode("root", "Node"); * //創建一個帶值的節點 * //obj.CreatXmlNode("root", "Node", "帶值的節點"); * //創建一個僅帶屬性的節點 * //obj.CreatXmlNode("root", "Node", "Attribe", "屬性值"); * //創建一個僅帶兩個屬性值的節點 * //obj.CreatXmlNode("root", "Node", "Attribe", "屬性值", "Attribe2", "屬性值2"); * //創建一個帶屬性值的節點值的節點 * // obj.CreatXmlNode("root", "Node", "Attribe", "屬性值","節點值"); * //在當前節點插入帶兩個屬性值的節點 * obj.CreatXmlNode("root", "Node", "Attribe", "屬性值", "Attribe2", "屬性值2","節點值"); * obj.XmlSave(xmlPath); * obj.Dispose(); ************************************************/ #region 創建一個只有聲明和根節點的XML文檔 /**//// <summary> /// 創建一個只有聲明和根節點的XML文檔 /// </summary> /// <param name="root"></param> public void CreateXmlRoot(string root) { //加入XML的聲明段落 xmlNode = xmlDoc.CreateNode(XmlNodeType.XmlDeclaration, "", ""); xmlDoc.AppendChild(xmlNode); //加入一個根元素 xmlElem = xmlDoc.CreateElement("", root, ""); xmlDoc.AppendChild(xmlElem); } #endregion #region 在當前節點下插入一個空節點節點 /**//// <summary> /// 在當前節點下插入一個空節點節點 /// </summary> /// <param name="mainNode">當前節點路徑</param> /// <param name="node">節點名稱</param> public void CreatXmlNode(string mainNode, string node) { XmlNode MainNode = xmlDoc.SelectSingleNode(mainNode); XmlElement objElem = xmlDoc.CreateElement(node); MainNode.AppendChild(objElem); } #endregion #region 在當前節點插入一個僅帶值的節點 /**//// <summary> /// 在當前節點插入一個僅帶值的節點 /// </summary> /// <param name="mainNode">當前節點</param> /// <param name="node">新節點</param> /// <param name="content">新節點值</param> public void CreatXmlNode(string mainNode, string node, string content) { XmlNode MainNode = xmlDoc.SelectSingleNode(mainNode); XmlElement objElem = xmlDoc.CreateElement(node); objElem.InnerText = content; MainNode.AppendChild(objElem); } #endregion #region 在當前節點的插入一個僅帶屬性值的節點 /**//// <summary> /// 在當前節點的插入一個僅帶屬性值的節點 /// </summary> /// <param name="MainNode">當前節點或路徑</param> /// <param name="Node">新節點</param> /// <param name="Attrib">新節點屬性名稱</param> /// <param name="AttribValue">新節點屬性值</param> public void CreatXmlNode(string MainNode, string Node, string Attrib, string AttribValue) { XmlNode mainNode = xmlDoc.SelectSingleNode(MainNode); XmlElement objElem = xmlDoc.CreateElement(Node); objElem.SetAttribute(Attrib, AttribValue); mainNode.AppendChild(objElem); } #endregion #region 創建一個帶屬性值的節點值的節點 /**//// <summary> /// 創建一個帶屬性值的節點值的節點 /// </summary> /// <param name="MainNode">當前節點或路徑</param> /// <param name="Node">節點名稱</param> /// <param name="Attrib">屬性名稱</param> /// <param name="AttribValue">屬性值</param> /// <param name="Content">節點傳情</param> public void CreatXmlNode(string MainNode, string Node, string Attrib, string AttribValue, string Content) { XmlNode mainNode = xmlDoc.SelectSingleNode(MainNode); XmlElement objElem = xmlDoc.CreateElement(Node); objElem.SetAttribute(Attrib, AttribValue); objElem.InnerText = Content; mainNode.AppendChild(objElem); } #endregion #region 在當前節點的插入一個僅帶2個屬性值的節點 /**//// <summary> /// 在當前節點的插入一個僅帶2個屬性值的節點 /// </summary> /// <param name="MainNode">當前節點或路徑</param> /// <param name="Node">節點名稱</param> /// <param name="Attrib">屬性名稱一</param> /// <param name="AttribValue">屬性值一</param> /// <param name="Attrib2">屬性名稱二</param> /// <param name="AttribValue2">屬性值二</param> public void CreatXmlNode(string MainNode, string Node, string Attrib, string AttribValue, string Attrib2, string AttribValue2) { XmlNode mainNode = xmlDoc.SelectSingleNode(MainNode); XmlElement objElem = xmlDoc.CreateElement(Node); objElem.SetAttribute(Attrib, AttribValue); objElem.SetAttribute(Attrib2, AttribValue2); mainNode.AppendChild(objElem); } #endregion #region 在當前節點插入帶兩個屬性的節點 /**//// <summary> /// 在當前節點插入帶兩個屬性的節點 /// </summary> /// <param name="MainNode">當前節點或路徑</param> /// <param name="Node">節點名稱</param> /// <param name="Attrib">屬性名稱一</param> /// <param name="AttribValue">屬性值一</param> /// <param name="Attrib2">屬性名稱二</param> /// <param name="AttribValue2">屬性值二</param> /// <param name="Content">節點值</param> public void CreatXmlNode(string MainNode, string Node, string Attrib, string AttribValue, string Attrib2, string AttribValue2, string Content) { XmlNode mainNode = xmlDoc.SelectSingleNode(MainNode); XmlElement objElem = xmlDoc.CreateElement(Node); objElem.SetAttribute(Attrib, AttribValue); objElem.SetAttribute(Attrib2, AttribValue2); objElem.InnerText = Content; mainNode.AppendChild(objElem); } #endregion #region 保存Xml /**//// <summary> /// 保存Xml /// </summary> /// <param name="path">保存的當前路徑</param> public void XmlSave(string path) { xmlDoc.Save(path); } #endregion #endregion #region 根據父節點屬性值讀取子節點值 /**//************************************************** * 函數名稱:GetSubElementByAttribute(string XmlPath, string FatherElenetName, string AttributeName, int AttributeIndex, int ArrayLength) * 功能說明:根據父節點屬性值讀取子節點值 * 參 數: XmlPath:xml路徑;FatherElenetName:父節點名;AttributeName:屬性值;AttributeIndex:屬性索引;ArrayLength:要返回的節點數組長度 * 適應用Xml: * ArrayList al = new ArrayList(); * al = EC.XmlObject.GetSubElementByAttribute(XmlPath, "page", "/index.aspx", 0, 3); * for (int i = 0; i < al.Count; i++) * { * Response.Write(al[i].ToString()); * Response.Write("<br>"); * } ************************************************/ /**//// <summary> /// 根據父節點屬性讀取字節點值 /// </summary> /// <param name="XmlPath">xml路徑</param> /// <param name="FatherElenetName">父節點名</param> /// <param name="AttributeName">屬性值</param> /// <param name="AttributeIndex">屬性索引</param> /// <param name="ArrayLength">要返回的節點數組長度</param> /// <returns></returns> public static System.Collections.ArrayList GetSubElementByAttribute(string XmlPath, string FatherElenetName, string AttributeName, int AttributeIndex, int ArrayLength) { System.Collections.ArrayList al = new System.Collections.ArrayList(); XmlDocument docXml = new XmlDocument(); docXml.Load(@XmlPath); XmlNodeList xn = docXml.DocumentElement.ChildNodes; //遍歷第一層節點 foreach (XmlElement element in xn) { //判斷父節點是否為指定節點 if (element.Name == FatherElenetName) { //判斷父節點屬性的索引是否大于指定索引 if (element.Attributes.Count < AttributeIndex) return null; //判斷父節點的屬性值是否等于指定屬性 if (element.Attributes[AttributeIndex].Value == AttributeName) { XmlNodeList xx = element.ChildNodes; if (xx.Count > 0) { for (int i = 0; i < ArrayLength & i < xx.Count; i++) { al.Add(xx[i].InnerText); } } } } } return al; } #endregion #region 根據節點屬性讀取子節點值(較省資源模式) /**//************************************************** * 函數名稱:GetSubElementByAttribute(string XmlPath, string FatherElement, string AttributeName, string AttributeValue, int ArrayLength) * 功能說明:根據父節點屬性值讀取子節點值 * 參 數: XmlPath:xml路徑;FatherElenetName:父節點名;AttributeName:屬性名;AttributeValue:屬性值;ArrayLength:要返回的節點數組長度 * 適應用Xml: * ArrayList al = new ArrayList(); * al = EC.XmlObject.GetSubElementByAttribute(XmlPath, "page", "@name", "/index.aspx", 3); * for (int i = 0; i < al.Count; i++) * { * Response.Write(al[i].ToString()); * Response.Write("<br>"); * } ************************************************/ /**//// <summary> /// 根據節點屬性讀取子節點值(較省資源模式) /// </summary> /// <param name="XmlPath">xml路徑</param> /// <param name="FatherElement">父節點值</param> /// <param name="AttributeName">屬性名稱</param> /// <param name="AttributeValue">屬性值</param> /// <param name="ArrayLength">返回的數組長度</param> /// <returns></returns> public static System.Collections.ArrayList GetSubElementByAttribute(string XmlPath, string FatherElement, string AttributeName, string AttributeValue, int ArrayLength) { System.Collections.ArrayList al = new System.Collections.ArrayList(); XmlDocument docXml = new XmlDocument(); docXml.Load(@XmlPath); XmlNodeList xn; xn = docXml.DocumentElement.SelectNodes("//" + FatherElement + "[" + @AttributeName + "='" + AttributeValue + "']"); XmlNodeList xx = xn.Item(0).ChildNodes; for (int i = 0; i < ArrayLength & i < xx.Count; i++) { al.Add(xx.Item(i).InnerText); } return al; } #endregion } }
關于c#中xml操作類的方法就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。