Write an XML operation class, including reading, inserting, modifying, and deleting. From: 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> // Summary of xmlhelper /// </Summary> public class xmlhelper {public xmlhelper () {}/// <summary> /// read data /// </Summary> /// <Param name = "path"> path </param> /// <Param name = "Node"> node </param> /// <Param name =" attribute "> attribute name, this attribute value is returned if it is not null, otherwise, the series value </param> // <returns> string </returns>/********************** is returned /******************** * ******************************* use the display column: * 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> /// insert data /// </Summary> /// <Param name = "path"> path </param> /// <Param name = "Node"> node </param> // <Param name = "element"> element name, insert a new element if it is not empty. Otherwise, insert the attribute </param> // <Param name = "attribute"> attribute name into the element. If it is not empty, insert the attribute value, otherwise, insert an element value </param> /// <Param name = "value"> 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 (); D OC. 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> /// modify data /// </Summary> /// <Param name = "path"> path </param> // /<Param name = "Node"> node </param> // <Param name = "attribute"> attribute name, when not empty, modify the node attribute value, otherwise, modify the node value </param> /// <Param name = "value"> value </param> /// <returns> </returns> /***** **************************************** * ***** use the display column: * 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> /// delete data /// </Summary> /// <Param name = "path"> path </param> // /<Param name = "Node"> node </param> // <Param name = "attribute"> attribute name, this node attribute value is deleted if it is not empty, otherwise, delete the node value </param> /// <Param name = "value"> value </param> /// <returns> </returns> /***** **************************************** * ***** use the display column: * 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/>
========================================================== ============
Usage:
String xml = server. mappath ("xmlfile. xml ");
// Insert element
// Xmlhelper. insert (XML, "/root", "Studio ","","");
// Insert element/attribute
// Xmlhelper. insert (XML, "/root/Studio", "Site", "name", "path Studio ");
// Xmlhelper. insert (XML, "/root/Studio", "Site", "name", "clove fish Studio ");
// Xmlhelper. insert (XML, "/root/Studio", "Site", "name", "spectral Tiancheng Studio ");
// Xmlhelper. insert (XML, "/root/studio/Site [@ name = 'spectrum city studio']", "Master", "", "Red Dust meditation ");
// Insert attributes
// Xmlhelper. insert (XML, "/root/studio/Site [@ name = 'xiaolaizao']", "", "url ",
Http://www.jecda.com );
// Xmlhelper. insert (XML, "/root/studio/Site [@ name = 'clove fish studio']", "", "url ",
Http://www.jecda.com );
// Xmlhelper. insert (XML, "/root/studio/Site [@ name = 'shutiancheng studio ']", "", "url ",
Http://www.jecda.com );
// Modify the element value
// Xmlhelper. Update (XML, "/root/studio/Site [@ name = 'shutiancheng studio ']/master", "", "reddust ");
// Modify the attribute value
// Xmlhelper. Update (XML, "/root/studio/Site [@ name = 'shutiancheng studio ']", "url ",
Http://www.jecda.com );
// Xmlhelper. Update (XML, "/root/studio/Site [@ name = 'shutiancheng studio ']", "name", "putiancheng Studio ");
// Read the element value
// Response. write ("<div>" + xmlhelper. read (XML, "/root/studio/site/master", "") + "</div> ");
// Read the attribute value
// Response. Write ("<div>" + xmlhelper. Read (XML, "/root/studio/Site", "url") + "</div> ");
// Read specific attribute values
// Response. write ("<div>" + xmlhelper. read (XML, "/root/studio/Site [@ name = 'clove fish studio']", "url") + "</div> ");
// Delete attributes
// Xmlhelper. Delete (XML, "/root/studio/Site [@ name = 'xiaolaizao']", "url ");
// Delete an element
// 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>
For example, if multiple nodes have the same name, the call parameters should be written as follows:
String dir = xmlhelper. Read (file, "config/prop [@ key = 'dpmdatadir ']", "value ");
You can use "config/prop [@ key = 'dpmdatadir ']" to directly locate the desired node.