XML Helper XML操作類

來源:互聯網
上載者:User
寫的一個XML操作類,包括讀取/插入/修改/刪除。轉自:http://blog.163.com/sc_g_ruan/blog/static/15032602011510105333868/
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>        /**************************************************         * 使用示列:         * 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;        }        /// <summary>        /// 插入資料        /// </summary>        /// <param name="path">路徑</param>        /// <param name="node">節點</param>        /// <param name="element">元素名,非空時插入新元素,否則在該元素中插入屬性</param>        /// <param name="attribute">屬性名稱,非空時插入該元素屬性值,否則插入元素值</param>        /// <param name="value">值</param>        /// <returns></returns>        /**************************************************         * 使用示列:         * 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 { }        }        /// <summary>        /// 修改資料        /// </summary>        /// <param name="path">路徑</param>        /// <param name="node">節點</param>        /// <param name="attribute">屬性名稱,非空時修改該節點屬性值,否則修改節點值</param>        /// <param name="value">值</param>        /// <returns></returns>        /**************************************************         * 使用示列:         * 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 { }        }        /// <summary>        /// 刪除資料        /// </summary>        /// <param name="path">路徑</param>        /// <param name="node">節點</param>        /// <param name="attribute">屬性名稱,非空時刪除該節點屬性值,否則刪除節點值</param>        /// <param name="value">值</param>        /// <returns></returns>        /**************************************************         * 使用示列:         * 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:
<?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.jecda.com);
//XmlHelper.Insert(xml, "/Root/Studio/Site[@Name='丁香魚工作室']", "", "Url",
http://www.jecda.com);
//XmlHelper.Insert(xml, "/Root/Studio/Site[@Name='譜天城工作室']", "", "Url",
http://www.jecda.com);
//修改元素值
//XmlHelper.Update(xml, "/Root/Studio/Site[@Name='譜天城工作室']/Master", "", "RedDust");
//修改屬性值
//XmlHelper.Update(xml, "/Root/Studio/Site[@Name='譜天城工作室']", "Url",
http://www.jecda.com);
//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", "");

 

<?xml version="1.0" encoding="UTF-8"?><config>  <prop key="allServiceOnOneServer" value="true" type="Boolean" />  <prop key="backgroundMapUrl" value="http://%hostname%:8081/ArcGIS/rest/services/DPMRasterMap/MapServer" type="String" />  <prop key="videoMapUrl" value="http://%hostname%:8081/ArcGIS/rest/services/DPM_Map_Ags/MapServer" type="String" />  <prop key="queryMapUrl" value="http://%hostname%:8081/ArcGIS/rest/services/DPM_Map_Ags/MapServer" type="String" />  <prop key="fmsurl" value="11111111111" type="String" />  <prop key="wsdlurl" value="http://%hostname%:8080/DPMService.asmx?WSDL" type="String" />  <prop key="dpmdatadir" value="http://%hostname%:8083/dpmdata/CQ_ALL/xml" type="String" /></config>

像這種有多個節點名稱相同的,調用參數應該這麼寫:

string dir = XmlHelper.Read(file, "config/prop[@key='dpmdatadir']", "value");

通過"config/prop[@key='dpmdatadir']"就可以直接定位到想要的節點,感覺好爽

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.