C#完成的XML操作類實例。本站提示廣大學習愛好者:(C#完成的XML操作類實例)文章只能為提供參考,不一定能成為您想要的結果。以下是C#完成的XML操作類實例正文
本文實例講述了C#完成的XML操作類。分享給年夜家供年夜家參考。詳細以下:
這裡講述了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.Xml; namespace PuTianCheng { /// <summary> /// XmlHelper 的摘要解釋 /// </summary> public class XmlHelper { public XmlHelper() { } /// <summary> /// 讀取數據 /// </summary> /// <param name="path">途徑</param> /// <param name="node">節點</param> /// <param name="attribute">屬性名,非空時前往該屬性值,不然前往串連值</param> /// <returns>string</returns> 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; } /// <summary> /// 拔出數據 /// </summary> /// <param name="path">途徑</param> /// <param name="node">節點</param> /// <param name="element">元素名,非空時拔出新元素,不然在該元素中拔出屬性</param> /// <param name="attribute">屬性名,非空時拔出該元素屬性值,不然拔出元素值</param> /// <param name="value">值</param> /// <returns></returns> 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_x(element); if (attribute.Equals("")) xe.InnerText = value; else xe.SetAttribute(attribute, value); xn.AppendChild(xe); } doc.Save(path); } catch { } } /// <summary> /// 修正數據 /// </summary> /// <param name="path">途徑</param> /// <param name="node">節點</param> /// <param name="attribute">屬性名,非空時修正該節點屬性值,不然修正節點值</param> /// <param name="value">值</param> /// <returns></returns> 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 { } } /// <summary> /// 刪除數據 /// </summary> /// <param name="path">途徑</param> /// <param name="node">節點</param> /// <param name="attribute">屬性名,非空時刪除該節點屬性值,不然刪除節點值</param> /// <param name="value">值</param> /// <returns></returns> 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:
<?xml version="1.0" encoding="utf-8"?> <Root />
應用辦法:
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.putiancheng.com/"); //修正元素值 XmlHelper.Update(xml, "/Root/Studio/Site[@Name='譜天城任務室']/Master", "", "RedDust"); //修正屬性值 XmlHelper.Update(xml, "/Root/Studio/Site[@Name='譜天城任務室']", "Url", "http://www.putiancheng.net/"); XmlHelper.Update(xml, "/Root/Studio/Site[@Name='譜天城任務室']", "Name", "PuTianCheng Studio"); //讀取元素值 Response.Write("<div>" + XmlHelper.Read(xml, "/Root/Studio/Site/Master", "") + "</div>"); //讀取屬性值 Response.Write("<div>" + XmlHelper.Read(xml, "/Root/Studio/Site", "Url") + "</div>"); //讀取特定屬性值 Response.Write("<div>" + XmlHelper.Read(xml, "/Root/Studio/Site[@Name='丁噴鼻魚任務室']", "Url") + "</div>"); //刪除屬性 XmlHelper.Delete(xml, "/Root/Studio/Site[@Name='巷子任務室']", "Url"); //刪除元素 XmlHelper.Delete(xml, "/Root/Studio", "");
願望本文所述對年夜家的C#法式設計有所贊助。