Public class xmlcontrol
{
Protected string strxmlfile;
Protected xmldocument objxmldoc = new xmldocument ();
Public xmlcontrol (string xmlfile)
{
//
// Todo:Add the constructorProgramCode
//
Try
{
Objxmldoc. Load (xmlfile );
}
Catch (system. Exception ex)
{
Throw ex;
}
Strxmlfile = xmlfile;
}
Public dataview getdata (string xmlpathnode)
{
//Search for data. ReturnsDataview
Dataset DS = new dataset ();
Stringreader READ = new stringreader (objxmldoc. selectsinglenode (xmlpathnode). outerxml );
DS. readxml (read );
Return Ds. Tables [0]. defaultview;
}
Public void Replace (string xmlpathnode, string content)
{
//Update the node content.
Objxmldoc. selectsinglenode (xmlpathnode). innertext = content;
}
Public void Delete (string node)
{
//Delete a node.
String mainnode = node. substring (0, node. lastindexof ("/"));
Objxmldoc. selectsinglenode (mainnode). removechild (objxmldoc. selectsinglenode (node ));
}
Public void insertnode (string mainnode, string childnode, string element, string content)
{
//Insert a node and a subnode of the node.
Xmlnode objrootnode = objxmldoc. selectsinglenode (mainnode );
Xmlelement objchildnode = objxmldoc. createelement (childnode );
Objrootnode. appendchild (objchildnode );
Xmlelement objelement = objxmldoc. createelement (element );
Objelement. innertext = content;
Objchildnode. appendchild (objelement );
}
Public void insertelement (string mainnode, string element, string attrib, string attribcontent, string content)
{
//Insert a node with a property.
Xmlnode objnode = objxmldoc. selectsinglenode (mainnode );
Xmlelement objelement = objxmldoc. createelement (element );
Objelement. setattribute (attrib, attribcontent );
Objelement. innertext = content;
Objnode. appendchild (objelement );
}
Public void insertelement (string mainnode, string element, string content)
{
//Insert a node without attributes.
Xmlnode objnode = objxmldoc. selectsinglenode (mainnode );
Xmlelement objelement = objxmldoc. createelement (element );
Objelement. innertext = content;
Objnode. appendchild (objelement );
}
Public void save ()
{
//Save the token.
Try
{
Objxmldoc. Save (strxmlfile );
}
Catch (system. Exception ex)
{
Throw ex;
}
Objxmldoc = NULL;
}
}
========================================================== ========================
Instance application:
String strxmlfile = server. mappath ("testxml. xml ");
Xmlcontrol xmltool = new xmlcontrol (strxmlfile );
//Data Visualization
// Dglist. datasource = xmltool. getdata ("book/authors [ISBN =" 0002 "]");
// Dglist. databind ();
//Update Element Content
// Xmltool. Replace ("book/authors [ISBN =" 0002 "]/content", "ppppppp ");
// Xmltool. Save ();
//Add a new node
// Xmltool. insertnode ("book", "author", "ISBN", "0004 ");
// Xmltool. insertelement ("book/author [ISBN =" 0004 "]", "content", "aaaaaaaaa ");
// Xmltool. insertelement ("book/author [ISBN =" 0004 "]", "title", "sex", "man", "iiiiiiii ");
// Xmltool. Save ();
//Deletes all content and attributes of a specified node.
// Xmltool. Delete ("book/author [ISBN =" 0004 "]");
// Xmltool. Save ();
// delete a subnode of a specified node
// xmltool. delete ("book/authors [ISBN =" 0003 "]");
// xmltool. save ();