public class Xmlcontrol
{
protected string Strxmlfile;
protected XmlDocument objxmldoc = new XmlDocument ();
Public Xmlcontrol (String XmlFile)
{
//
TODO: Add the constructor program code here
//
Try
{
Objxmldoc.load (XmlFile);
}
catch (System.Exception ex)
{
Throw ex;
}
Strxmlfile = XmlFile;
}
Public DataView GetData (string xmlpathnode)
{
Find data. 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 node contents.
Objxmldoc.selectsinglenode (Xmlpathnode). innertext = Content;
}
public void Delete (string Node)
{
Deletes 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)
{
Inserts a node and a child node of this 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)
{
Inserts 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)
{
Inserts a node with no attributes.
XmlNode objnode = Objxmldoc.selectsinglenode (Mainnode);
XmlElement objelement = objxmldoc.createelement (Element);
Objelement.innertext = Content;
Objnode.appendchild (objelement);
}
public void Save ()
{
Save the document file.
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 display
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 for a specified node
Xmltool.delete ("book/author[isbn=/" 0004/"]");
Xmltool.save ();
Deletes a child node of a specified node
Xmltool.delete ("book/authors[isbn=/" 0003/"]");
Xmltool.save ();