Public class xmlcontrol
{
Protected string strxmlfile;
Protected xmldocument objxmldoc = new xmldocument ();
Public xmlcontrol (string xmlfile)
{< br> //
// todo: add the constructor program here
//
try
{< br> objxmldoc. load (xmlfile);
}< br> catch (system. exception ex)
{< br> throw ex;
}< br> strxmlfile = xmlfile;
}
Public dataview getdata (string xmlpathnode)
{
// Search for data records. Returns a dataview.
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 volume content.
Objxmldoc. selectsinglenode (xmlpathnode). innertext = content;
}
Public void Delete (string node)
{< br> // delete a vertex.
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)
{< br> // insert a vertex and a subvertex Of This vertex.
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)
{< br> // insert a vertex, it has a unique modularity.
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)
{< br> // insert a vertex without any attention.
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 transfer history
// 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 vertex
// 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 ();
// All content and attention of a specified Vertex
// Xmltool. Delete ("book/author [ISBN = \" 0004 \ "]");
// Xmltool. Save ();
// Delete a subvertex of a specified Vertex
// Xmltool. Delete ("book/authors [ISBN = \" 0003 \ "]");
// Xmltool. Save ();