Everyone wants to know XML, using XML technology to store data and documentation is a very easy thing, .NET Framework provides a class xmlDocument that can be operated in its namespace system.xml, it It is very easy to use, XMLDocument is actually a simple tree. The following is a detailed introduction to the use of XMLDocument.
Below is a common method of operating nodes in this class.
// create a new node in the document object from the source node // and name it as "sName" // the return value indicates success or failurepublic bool AddNode (XmlNode oSource, String sName); // same as above except that it also specifies the parent node of the // newly created node // the return value indicates success or failure (returns false if the // parent node does not exist) public bool AddNode (XmlNode oSource, String sName, String sParent); // create a set of new nodes in the document object from the source node // list and name them as "sName" // the return value indicates success or failurepublic bool AddNodes (XmlNodeList oSourceList, String sName); // same as above except that it also specifies the parent node of the // newly created nodes the return value indicates success or failure // (returns false if the parent node // does not exist) public bool AddNodes (XmlNodeList oSourceList, String sName, String sParent); / / merge the source node into a node named "s Name "in the document object // the node named" sName "will be created if it does not exist // the return value indicates success or failurepublic bool MergeNode (XmlNode oSource, String sName); // same as above except that it also specifies the parent node of the merged node // the return value indicates success or failure (returns false if the parent node // does not exist) public bool MergeNode (XmlNode oSource, String sName, String sParent);
Let's give an example of increasing nodes
DocVechile.xml
Record>
Record>
Record>
Vehicledata>
DOCDRIVER.XML
Record>
Record>
Record>
Driverdata>
The following code will add a node:
Dim mydoc as xmldocumentex = new xmldocumentex ()
MyDoc.loadxml (" data>")
MyDoc.addnode (Docvehi.Selectsinglenode ("// Record"), "Vehicles", "DATA")
MYDOC.ADDNode (DOCDRIVER.SELECTSIINODE ("// Record"), "DriverRecord", "Data")
MYDOC.XML
Vehicle record>
Driverrecord>
Data> We can also add all records of a recordset to nodes using the AddNodes method: DIM MYDOC AS XMLDocumentex = new xmldocumentex () MyDoc.LoadXML ("
MyDoc.addnodes (DocveHicle.SelectNodes ("// Record"), "VehicleRecord", "VEHICLE DATA")
MyDoc.addnodes ("// record", "driverrecord", "driverData") The results are as follows: MyDoc.xml
VehiclesRecord>
VehiclesRecord>
VehiclesRecord>
Vehicledata>
Driverrecord>
Driverrecord>
Driverrecord>
Driverdata>
Data> I will introduce how to merge nodes. Suppose we have two XMLDocument files DocBook1 and DocBook2, which contains
MyDoc.mergenode (Docbook1.Selectsinglenode ("// Book"), "BOOK", "DATA")
MyDoc.mergenode ("// // book"), "Book", "DATA") The following effects are as follows: MYDOC.XML
Book>
Data> The following is all source code: Sealed Public Class XMLDocumentex: XmLDocument
{
Public Bool AddNode (XMLNode Osource, String Sname)
{
Return AddNode (Osource, Sname, Null);
}
Public Bool AddNode (XMLNode Osource, String Sname, String Sparent)
{
Try
{
IF (sname! = null && oysce! = NULL)
{
// Create the New Node with Given Name
XMLNode OnewNode = CreateElement (SNAME);
// Copy THE Contents from the Source Node
Onewnode.innerxml = OSource.innerXML;
// if there is no pient node specified, then add
// the new node as a child node of the root node
IF (Sparent! = null) sparent = sparent.trim ();
IF (sparent == null || sparent.equals (string.empty)))
{
DocumentElement.Appendchild (OnewNode);
Return True;
}
// OtherWise Add the New Node as a child of the parent node
Else
{
IF (! sparent.substring (0,2). Equals ("//")) sparent = "//" sparent;
XMLNode Oparent = Selectsinglenode (Sparent);
IF (Oparent! = NULL)
{
Oparent.Appendchild (OnewNode);
Return True;
}
}
}
}
Catch (Exception)
{
// Error Handling Code
}
Return False;
}
Public Bool AddNodes (XMLNodelist OsourceList, String Sname)
{
Return AddNodes (OsourceList, Sname, Null);
}
Public Bool Addnodes (XMLNodelist OsourceList, String Sname, String Sparent)
{
Try
{
IF (OsourceList! = NULL)
{
// Call Addnode for Each Item in The Source Node List
// Return True Only All Nodes Are Added SuccessFully
INT i = 0;
While (i { IF (! AddNode (OsourceList.item (i), Sname, Sparent) Return False; i ; } Return True; } } Catch (Exception) { // Error Handling Code } Return False; } Public Bool Mergenode (XMLNode Osource, String Sname) { Return Mergenode (Osource, Sname, Null); } Public Bool Mergenode (XMLNode Osource, String Sname, String Sparent) { Try { IF (sname! = null && oysce! = NULL) { XMLNode THENODE = NULL; // if there is no parent node specified ... IF (Sparent! = null) sparent = sparent.trim (); IF (sparent == null || sparent.equals (string.empty))) { // if the node with specified name does not exist, // add it as a child node of the root node THENODE = Selectsinglenode ("//" sname); IF (THENODE == NULL) { THENODE = CREATEEELEMENT (SNAME); DocumentElement.Appendchild (THENODE); } } // if The Parent Node IS Specified ... Else { // Find the Parent Node IF (! sparent.substring (0,2). Equals ("//")) sparent = "//" sparent; XMLNode theparent = selectsinglenode (sparent); if (theparent! = Null) { // if the node with specified name does not exist, crete // IT First, Then Add it as a child node of the parent node Thenode = theparent.selectsinglenode (Sname); IF (THENODE == NULL) { THENODE = CREATEEELEMENT (SNAME); Theparent.Appendchild (THENODE); } } } // Merge the Content of the Source Node INTO // the node with specified name IF (THENODE! = NULL) { Thenode.innerxml = Osource.innerXML; Return True; } } } Catch (Exception) { } Return False; } }