這篇文章主要介紹了C#-XML操作類的示例分析,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
創(chuàng)新互聯(lián)公司是專業(yè)的昌江黎族網(wǎng)站建設(shè)公司,昌江黎族接單;提供成都網(wǎng)站建設(shè)、網(wǎng)站建設(shè),網(wǎng)頁設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行昌江黎族網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!
寫的一個(gè)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.Xml; namespace PuTianCheng { ////// XmlHelper 的摘要說明 /// public class XmlHelper { public XmlHelper() { } ////// 讀取數(shù)據(jù) /// /// 路徑 /// 節(jié)點(diǎn) /// 屬性名,非空時(shí)返回該屬性值,否則返回串聯(lián)值 ///string /************************************************** * 使用示列: * XmlHelper.Read(path, "/Node", "") * XmlHelper.Read(path, "/Node/Element[@Attribute='Name']", "Attribute") ************************************************/ public static string Read(string path, string node, string attribute) { string value = ""; try { XmlDocument doc = new XmlDocument(); doc.Load(path); XmlNode xn = doc.SelectSingleNode(node); value = (attribute.Equals("") ? xn.InnerText : xn.Attributes[attribute].Value); } catch { } return value; } ////// 插入數(shù)據(jù) /// /// 路徑 /// 節(jié)點(diǎn) /// 元素名,非空時(shí)插入新元素,否則在該元素中插入屬性 /// 屬性名,非空時(shí)插入該元素屬性值,否則插入元素值 /// 值 ////************************************************** * 使用示列: * XmlHelper.Insert(path, "/Node", "Element", "", "Value") * XmlHelper.Insert(path, "/Node", "Element", "Attribute", "Value") * XmlHelper.Insert(path, "/Node", "", "Attribute", "Value") ************************************************/ public static void Insert(string path, string node, string element, string attribute, string value) { try { XmlDocument doc = new XmlDocument(); doc.Load(path); XmlNode xn = doc.SelectSingleNode(node); if (element.Equals("")) { if (!attribute.Equals("")) { XmlElement xe = (XmlElement)xn; xe.SetAttribute(attribute, value); } } else { XmlElement xe = doc.CreateElement(element); if (attribute.Equals("")) xe.InnerText = value; else xe.SetAttribute(attribute, value); xn.AppendChild(xe); } doc.Save(path); } catch { } } /// /// 修改數(shù)據(jù) /// /// 路徑 /// 節(jié)點(diǎn) /// 屬性名,非空時(shí)修改該節(jié)點(diǎn)屬性值,否則修改節(jié)點(diǎn)值 /// 值 ////************************************************** * 使用示列: * XmlHelper.Insert(path, "/Node", "", "Value") * XmlHelper.Insert(path, "/Node", "Attribute", "Value") ************************************************/ public static void Update(string path, string node, string attribute, string value) { try { XmlDocument doc = new XmlDocument(); doc.Load(path); XmlNode xn = doc.SelectSingleNode(node); XmlElement xe = (XmlElement)xn; if (attribute.Equals("")) xe.InnerText = value; else xe.SetAttribute(attribute, value); doc.Save(path); } catch { } } /// /// 刪除數(shù)據(jù) /// /// 路徑 /// 節(jié)點(diǎn) /// 屬性名,非空時(shí)刪除該節(jié)點(diǎn)屬性值,否則刪除節(jié)點(diǎn)值 /// 值 ////************************************************** * 使用示列: * XmlHelper.Delete(path, "/Node", "") * XmlHelper.Delete(path, "/Node", "Attribute") ************************************************/ public static void Delete(string path, string node, string attribute) { try { XmlDocument doc = new XmlDocument(); doc.Load(path); XmlNode xn = doc.SelectSingleNode(node); XmlElement xe = (XmlElement)xn; if (attribute.Equals("")) xn.ParentNode.RemoveChild(xn); else xe.RemoveAttribute(attribute); doc.Save(path); } catch { } } } }
==================================================
XmlFile.xml:
==================================================
使用方法:
string xml = Server.MapPath("XmlFile.xml"); //插入元素 //XmlHelper.Insert(xml, "/Root", "Studio", "", ""); //插入元素/屬性 //XmlHelper.Insert(xml, "/Root/Studio", "Site", "Name", "小路工作室"); //XmlHelper.Insert(xml, "/Root/Studio", "Site", "Name", "丁香魚工作室"); //XmlHelper.Insert(xml, "/Root/Studio", "Site", "Name", "五月軟件"); //XmlHelper.Insert(xml, "/Root/Studio/Site[@Name='五月軟件]", "Master", "", "五月"); //插入屬性 //XmlHelper.Insert(xml, "/Root/Studio/Site[@Name='小路工作室']", "", "Url", "http://www.wzlu.com/"); //XmlHelper.Insert(xml, "/Root/Studio/Site[@Name='丁香魚工作室']", "", "Url", "http://www.luckfish.net/"); //XmlHelper.Insert(xml, "/Root/Studio/Site[@Name='五月軟件]", "", "Url", "http://www.vs2005.com.cn/"); //修改元素值 //XmlHelper.Update(xml, "/Root/Studio/Site[@Name='五月軟件]/Master", "", "Wuyue"); //修改屬性值 //XmlHelper.Update(xml, "/Root/Studio/Site[@Name='五月軟件]", "Url", "http://www.vs2005.com.cn/"); //XmlHelper.Update(xml, "/Root/Studio/Site[@Name='五月軟件]", "Name", "MaySoft"); //讀取元素值 //Response.Write("" + XmlHelper.Read(xml, "/Root/Studio/Site/Master", "") + "
"); //讀取屬性值 //Response.Write("" + XmlHelper.Read(xml, "/Root/Studio/Site", "Url") + "
"); //讀取特定屬性值 //Response.Write("" + XmlHelper.Read(xml, "/Root/Studio/Site[@Name='丁香魚工作室']", "Url") + "
"); //刪除屬性 //XmlHelper.Delete(xml, "/Root/Studio/Site[@Name='小路工作室']", "Url"); //刪除元素 //XmlHelper.Delete(xml, "/Root/Studio", "");
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“C#-XML操作類的示例分析”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來學(xué)習(xí)!