ASP. net xml operation base class (modify, delete, add, create)
Using system. xml;
Namespace EC
{
///
/// XML operation base class
///
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
**************************************** ********/
///
/// Function: Read XML to Dataset
///
/// XML Path
/// Dataset
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, stringnode)
* 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:
*
* Ns1.everdns.com
*
* 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 "));
**************************************** ********/
///
/// Read the XML document and return a node: Applicable to level 1 nodes
///
/// XML Path
/// Node
///
Public static stringreadxmlreturnnode (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, stringxmlpathnode)
* 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 = newdataset ();
* DS = xmlobject. getxmldata (xmlpath, "root/items"); // read the current path
* This. gridview1.datasource = Ds;
* This. gridview1.databind ();
* Ds. Clear ();
* Ds. Dispose ();
* XML example:
*
*
*
* Http://www.paycenter.com.cn/cgi-bin/
* 80
*
*
**************************************** ********/
///
/// Query data, return all the lower-level nodes of the current node, and fill in a dataset
///
/// XML document path
/// Node path: Root Node/parent node/current node
///
Public static dataset getxmldata (string xmlpath, string xmlpathnode)
{
Xmldocument objxmldoc = new xmldocument ();
Objxmldoc. Load (xmlpath );
Dataset DS = new dataset ();
Stringreader READ = newstringreader (objxmldoc. selectsinglenode (xmlpathnode). outerxml );
DS. readxml (read );
Return Ds;
}
# Endregion
# Region update XML node content
/*************************************** ***********
* Function name: xmlnodereplace (string xmlpath, stringnode, string content)
* Function Description: update the content of an XML node.
* Parameter: xmlpath: XML document path; node: Path of the current node; content: content
* Use the display column:
* Using EC; // reference the namespace
* String xmlpath = server. mappath ("/ebdomainconfig/domainconfig. xml"); // obtain the XML Path
* Xmlobject. xmlnodereplace (xmlpath, "root/test", "56789"); // update node content
**************************************** ********/
///
/// Update the XML node content
///
/// XML Path
/// The node whose content you want to change: Node path root node/parent node/current node
/// New Content
Public static void xmlnodereplace (string xmlpath, string node, stringcontent)
{
Xmldocument objxmldoc = new xmldocument ();
Objxmldoc. Load (xmlpath );
Objxmldoc. selectsinglenode (node). innertext = content;
Objxmldoc. Save (xmlpath );
}
# Endregion
# Region delete an XML node and Its subnodes
/*************************************** ***********
* Function name: xmlnodedelete (string xmlpath, string node)
* Function Description: deletes an XML node and Its subnodes.
* Parameter: xmlpath: XML document path; node: 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
* Xmlobject. xmlnodedelete (xmlpath, "root/test"); // Delete the current node
**************************************** ********/
///
/// Delete the XML node and Its subnodes
///
/// XML document path
/// Node path
Public static void xmlnodedelete (string xmlpath, string node)
{
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