C #: XML operation class,
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>
/// 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, a series value is returned. </param>
/// <Returns> string </returns>
/*************************************** ***********
* 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. If it is not null, a new element is inserted. Otherwise, an attribute is inserted into the element. </param>
/// <Param name = "attribute"> attribute name. If it is not null, the attribute value of this element is inserted. Otherwise, the element value is inserted. </param>
/// <Param name = "value"> value </param>
/// <Returns> </returns>
/*************************************** ***********
* 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 {}
}
/// <Summary>
/// Modify data
/// </Summary>
/// <Param name = "path"> path </param>
/// <Param name = "node"> node </param>
/// <Param name = "attribute"> attribute name. If it is not null, the attribute value of the node is modified. Otherwise, the node value is modified. </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. If it is not null, the attribute value of the node is deleted. Otherwise, the node value is deleted. </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 = '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 = 'spectrum city studio']", "", "Url", "http://www.putiancheng.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 = 'spectrum city studio']", "Url", "http://www.putiancheng.net /");
// 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 ","");