/*************************************** **************************************** ***
*
* Function Description: XML processing base class
* Author: Liu gongxun;
* Version: v0.1 (C #2.0); time: 2006-12-13
*
**************************************** ****************************************/
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. IO;
Using system. xml;
Using system. text;
Using msxml2;
Namespace EC
{
/// <Summary>
/// XML operation base class
/// </Summary>
Public class xmlobject: idisposable
{
// The following is a single-function static class
# Region read XML to Dataset
/*************************************** ***********
* Function name: getxml (string xmlpath)
* Function Description: Read XML to Dataset
* Parameter: xmlpath: XML document path
* Use the display column:
* Using EC; // reference the namespace
* String xmlpath = server. mappath ("/ebdnsconfig/dnsconfig. xml"); // obtain the XML Path
* Dataset DS = EC. xmlobject. getxml (xmlpath); // read XML to Dataset
**************************************** ********/
/// <Summary>
/// Function: Read XML to Dataset
/// </Summary>
/// <Param name = "xmlpath"> XML Path </param>
/// <Returns> dataset </returns>
Public static dataset getxml (string xmlpath)
{
Dataset DS = new dataset ();
DS. readxml (@ xmlpath );
Return Ds;
}
# Endregion
# Region reads the XML document and returns a node
/*************************************** ***********
* Function name: readxmlreturnnode (string xmlpath, string node)
* Function Description: Read the XML document and return a node: Applicable to first-level nodes.
* Parameter: xmlpath: XML document path; node name returned by Node
* XML: <? XML version = "1.0" encoding = "UTF-8"?>
* <Root>
* <Dns1> ns1.everdns.com </dns1>
* </Root>
* Use the display column:
* Using EC; // reference the namespace
* String xmlpath = server. mappath ("/ebdnsconfig/dnsconfig. xml"); // obtain the XML Path
* Response. Write (xmlobject. readxmlreturnnode (xmlpath, "mailmanager "));
**************************************** ********/
/// <Summary>
/// Read the XML document and return a node: Applicable to level 1 nodes
/// </Summary>
/// <Param name = "xmlpath"> XML Path </param>
/// <Param name = "nodename"> node </param>
/// <Returns> </returns>
Public static string readxmlreturnnode (string xmlpath, string node)
{
Xmldocument docxml = new xmldocument ();
Docxml. Load (@ xmlpath );
Xmlnodelist xn = docxml. getelementsbytagname (node );
Return XN. Item (0). innertext. tostring ();
}
# Endregion
# Region searches for data and returns a dataset
/*************************************** ***********
* Function name: getxmldata (string xmlpath, string xmlpathnode)
* Function Description: searches for data, returns all subordinate nodes of the current node, and fills in a dataset.
* Parameter: xmlpath: XML document path; xmlpathnode: Path of the current node
* Use the display column:
* Using EC; // reference the namespace
* String xmlpath = server. mappath ("/ebdomainconfig/domainconfig. xml"); // obtain the XML Path
* Dataset DS = new dataset ();
* DS = xmlobject. getxmldata (xmlpath, "root/items"); // read the current path
* This. gridview1.datasource = Ds;
* This. gridview1.databind ();
* Ds. Clear ();
* Ds. Dispose ();
* XML example:
* <? XML version = "1.0" encoding = "UTF-8"?>
* <Root>
* <Items name = "xinnet">
* <URL> http://www.paycenter.com.cn/cgi-bin/ </URL>
* <Port> 80 </port>
* </Items>
* </Root>
**************************************** ********/
/// <Summary>
/// Query data, return all the lower-level nodes of the current node, and fill in a dataset
/// </Summary>
/// <Param name = "xmlpath"> XML document path </param>
/// <Param name = "xmlpathnode"> node path: Root Node/parent node/current node </param>
/// <Returns> </returns>
Public static dataset getxmldata (string xmlpath, string xmlpathnode)
{
Xmldocument objxmldoc = new xmldocument ();
Objxmldoc. Load (xmlpath );
Dataset DS = new dataset ();
Stringreader READ = new stringreader (objxmldoc. selectsinglenode (xmlpathnode). outerxml );
DS. readxml (read );
Return Ds;
}
# Endregion
# region update XML node content
/*************************** * *********************
* Function Name: xmlnodereplace (string xmlpath, string node, string content)
* Function Description: Update XML node content
* parameter: xmlpath: XML document path; node: path of the current node; content: content
* usage display column:
* using EC; // reference namespace
* string xmlpath = server. mappath ("/ebdomainconfig/domainconfig. XML "); // get the XML Path
* xmlobject. xmlnodereplace (xmlpath, "root/test", "56789 "); // update the node content
******************************** ****************/
///
// update the content of an XML node
/ ///
/// XML Path
/// node for content replacement: node path root node/parent node/current node
// new content
Public static void xmlnodereplace (string xmlpath, string node, string content)
{< br> xmldocument objxmldoc = new xmldocument ();
objxmldoc. load (xmlpath);
objxmldoc. selectsinglenode (node ). innertext = content;
objxmldoc. save (xmlpath);
}
# Endregion
# region Delete the XML node and Its subnodes
/********************* * ***************************
* Function Name: xmlnodedelete (string xmlpath, string node)
* Function Description: delete an XML node and Its subnodes
* parameter: xmlpath: XML document path; node: path of the current node;
* usage indication column:
* using EC; // reference namespace
* string xmlpath = server. mappath ("/ebdomainconfig/domainconfig. XML "); // get the XML Path
* xmlobject. xmlnodedelete (xmlpath, "root/test "); // Delete the current node
******************************** ***************/
///
/// Delete the XML node and child node
///
/// XML document path
// node path
Public static void xmlnodedelete (string xmlpath, string node)
{< br> xmldocument objxmldoc = new xmldocument ();
objxmldoc. load (xmlpath);
string mainnode = node. substring (0, node. lastindexof ("/");
objxmldoc. selectsinglenode (mainnode ). removechild (objxmldoc. selectsinglenode (node);
objxmldoc. save (xmlpath);
}
# Endregion
# Region inserts a node and Its byte points
/*************************************** ***********
* Function name: xmlinsertnode (string xmlpath, string mailnode, string childnode, string element, string content)
* Function Description: Insert a node and Its byte points.
* Parameter: xmlpath: XML document path; mailnode: Path of the current node; childnode: newly inserted node; element: subnode of the inserted node; content: content of the subnode
* Use the display column:
* Using EC; // reference the namespace
* String xmlpath = server. mappath ("/ebdomainconfig/domainconfig. xml"); // obtain the XML Path
* Xmlobject. xmlinsertnode (xmlpath, "root/test", "test1", "Test2", "Test content"); // insert a node and Its byte points
* The generated XML format is
* <Test>
* <Test1>
* <Test2> test content </Test2>
* </Test1>
* </Test>
**************************************** ********/
/// <Summary>
/// Insert a node and Its byte points
/// </Summary>
/// <Param name = "xmlpath"> XML Path </param>
/// <Param name = "mailnode"> current Node path </param>
/// <Param name = "childnode"> insert a node </param>
/// <Param name = "element"> insert a subnode of a node </param>
/// <Param name = "content"> subnode content </param>
Public static void xmlinsertnode (string xmlpath, string mailnode, string childnode, string element, string content)
{
Xmldocument objxmldoc = new xmldocument ();
Objxmldoc. Load (xmlpath );
Xmlnode objrootnode = objxmldoc. selectsinglenode (mailnode );
Xmlelement objchildnode = objxmldoc. createelement (childnode );
Objrootnode. appendchild (objchildnode );
Xmlelement objelement = objxmldoc. createelement (element );
Objelement. innertext = content;
Objchildnode. appendchild (objelement );
Objxmldoc. Save (xmlpath );
}
# Endregion
# Region inserts a node with a property
/*************************************** ***********
* Function name: xmlinsertelement (string xmlpath, string mainnode, string element, string attrib, string attribcontent, string content)
* Function Description: Insert a node with a property.
* Parameter: xmlpath: XML document path; mailnode: Path of the current node; element: newly inserted node; attrib: attribute name; attribcontent: attribute value; content: content of the node
* Use the display column:
* Using EC; // reference the namespace
* String xmlpath = server. mappath ("/ebdomainconfig/domainconfig. xml"); // obtain the XML Path
* Xmlobject. xmlinsertelement (xmlpath, "root/test", "test1", "attrib", "attribute value", "node content"); // insert a node with one attribute
* The generated XML format is
* <Test>
* <Test1 attrib = "attribute value"> node content </test1>
* </Test>
**************************************** ********/
/// <Summary>
/// Insert a node with a property
/// </Summary>
/// <Param name = "xmlpath"> XML document path </param>
/// <Param name = "mainnode"> current Node path </param>
/// <Param name = "element"> new node </param>
/// <Param name = "attrib"> attribute name </param>
/// <Param name = "attribcontent"> attribute value </param>
/// <Param name = "content"> new node value </param>
Public static void xmlinsertelement (string xmlpath, string mainnode, string element, string attrib, string attribcontent, string content)
{
Xmldocument objxmldoc = new xmldocument ();
Objxmldoc. Load (xmlpath );
Xmlnode objnode = objxmldoc. selectsinglenode (mainnode );
Xmlelement objelement = objxmldoc. createelement (element );
Objelement. setattribute (attrib, attribcontent );
Objelement. innertext = content;
Objnode. appendchild (objelement );
Objxmldoc. Save (xmlpath );
}
# Endregion
# Region inserts a node without attributes
/*************************************** ***********
* Function name: xmlinsertelement (string xmlpath, string mainnode, string element, string content)
* Function Description: Insert a node without attributes
* Parameter: xmlpath: XML document path; mailnode: Path of the current node; element: newly inserted node; content: content of the node
* Use the display column:
* Using EC; // reference the namespace
* String xmlpath = server. mappath ("/ebdomainconfig/domainconfig. xml"); // obtain the XML Path
* Xmlobject. xmlinsertelement (xmlpath, "root/test", "text1", "node content"); // insert a node without attributes
* The generated XML format is
* <Test>
* <Text1> node content </text1>
* </Test>
**************************************** ********/
Public static void xmlinsertelement (string xmlpath, string mainnode, string element, string content)
{
Xmldocument objxmldoc = new xmldocument ();
Objxmldoc. Load (xmlpath );
Xmlnode objnode = objxmldoc. selectsinglenode (mainnode );
Xmlelement objelement = objxmldoc. createelement (element );
Objelement. innertext = content;
Objnode. appendchild (objelement );
Objxmldoc. Save (xmlpath );
}
# Endregion
// Class that can be used only when an object is created
Private bool _ alreadydispose = false;
Private string xmlpath;
Private xmldocument xmldoc = new xmldocument ();
Private xmlnode;
Private xmlelement xmlelem;
# Region construction and interpretation
Public xmlobject ()
{
}
~ Xmlobject ()
{
Dispose ();
}
Protected virtual void dispose (bool isdisposing)
{
If (_ alreadydispose) return;
If (isdisposing)
{
//
}
_ Alreadydispose = true;
}
# Endregion
# Region idisposable Member
Public void dispose ()
{
Dispose (true );
GC. suppressfinalize (this );
}
# Endregion