Write an XML operation class, including reading, inserting, modifying, and deleting. Write an XML operation class, including reading, inserting, modifying, and deleting.
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 of XmlHelper ///Public class XmlHelper {public XmlHelper (){}////// Read data //////Path///Node///Attribute name. this attribute value is returned if it is not null. Otherwise, a series value is returned.///
String
/*************************************** * *********** 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 ;}////// Insert data //////Path///Node///Element name. if it is not Null, a new element is inserted. Otherwise, an attribute is inserted in the element.///Attribute name. if it is not null, the attribute value of this element is inserted. Otherwise, the element value is inserted.///Value///
/*************************************** * *********** Use the display column: * 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 {}}////// Modify data //////Path///Node///Attribute name. if it is not null, the attribute value of the node is modified. Otherwise, the node value is modified.///Value///
/*************************************** * *********** 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 {}}////// Delete data //////Path///Node///Attribute name. if it is not null, the node attribute value is deleted. Otherwise, the node value is deleted.///Value///
/*************************************** * *********** 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:
========================================================== ============
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", "May software"); // XmlHelper. insert (xml, "/Root/Studio/Site [@ Name = '2014 software]", "Master", "", "May "); // Insert attributes // XmlHelper. insert (xml, "/Root/Studio/Site [@ Name = 'pathons']", "", "Url", "http://www.wzlu.com/"); // XmlHelper. insert (xml, "/Root/Studio/Site [@ Name = 'clove fish studio']", "", "Url", "http://www.luckfish.net/"); // XmlHelper. insert (xml, "/Root/Studio/Site [@ Name = 'May software]", "", "Url", "http://www.vs2005.com.cn /"); // modify the element value // XmlHelper. update (xml, "/Root/Studio/Site [@ Name = '2014 software]/Master", "", "Wuyue"); // modify the attribute value // XmlHelper. update (xml, "/Root/Studio/Site [@ Name = 'May software]", "Url", "http://www.vs2005.com.cn/"); // XmlHelper. update (xml, "/Root/Studio/Site [@ Name = '2014 software]", "Name", "MaySoft"); // read the element value // Response. write (""+ XmlHelper. Read (xml,"/Root/Studio/Site/Master "," ") +"
"); // Read the property value // Response. Write (""+ XmlHelper. Read (xml,"/Root/Studio/Site "," Url ") +"
"); // Read the specific property value // Response. Write (""+ XmlHelper. Read (xml,"/Root/Studio/Site [@ Name = 'clove fish studio'] "," Url ") +"
"); // Delete attributes // XmlHelper. delete (xml, "/Root/Studio/Site [@ Name = 'xiaolu Studio ']", "Url"); // Delete element // XmlHelper. delete (xml, "/Root/Studio ","");
The above is a detailed description of the C #-XML operation code instance. For more information, see PHP Chinese network (www.php1.cn )!