Xml
You must all understand XML, it is easy to use XML technology to store data and documents, the. NET Framework provides a convenient class for manipulating XML in its namespace System.Xml XmlDocument, it is very easy to use, XmlDocument is actually a simple tree. The following is a detailed description of how XmlDocument is used.
The following are common methods for manipulating 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 failure
public 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 failure
public 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 ' sname ' in the Document object
The node named "Sname" is created if it does not exist
The return value indicates success or failure
public 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);
Here we give an example of adding a node
Docvechile.xml
<VehicleData> <Record> <id>1001</id> <make>Ford</make> <model>Escort</model> <year>1984</year> </Record> <Record> <id>1002</id> <make>Toyota</make> <model>Tercel</model> <year>1996</year> </Record> <Record> <id>1003</id> <make>Mazda</make> <model>GLC</model> <year>1985</year> </Record> </VehicleData>
Docdriver.xml
<DriverData> <Record> <id>1</id> <firstname>Albert</firstname> <lastname>Einstein</lastname> </Record> <Record> <id>2</id > <firstname>Clint</firstname> <lastname>Eastwood</lastname> </ record> <Record> <id>3</id> <firstname>James</firstname> <lastname>Bond</lastname> </Record></DriverData>
The following code adds a node:
Dim MyDoc as Xmldocumentex = New Xmldocumentex () mydoc.loadxml ("<Data></Data>") Mydoc.addnode ( Docvehicle.selectsinglenode ("//record"), "Vehiclerecord", "Data") Mydoc.addnode (Docdriver.selectsinglenode ("// Record ")," Driverrecord "," Data ")
Mydoc.xml
<Data> <VehicleRecord> <id>...</id> <make>...</make> < model>...</model> <year>...</year> </Vehicle record> <driverrecord > <id>...</id> <firstname>...</firstname> <lastname>...</ Lastname> </DriverRecord></Data>
We also use the AddNodes method to add all the records of a recordset to the node:
Dim MyDoc as Xmldocumentex = New Xmldocumentex () mydoc.loadxml ("<Data> <vehicledata></vehicle data> <DriverData></DriverData> </Data> ") Mydoc.addnodes (Docvehicle.selectnodes ("//record ")," Vehiclerecord "," Vehicle Data ") Mydoc.addnodes (Docdriver.selectnodes ("//record ")," Driverrecord "," Driverdata ")
The results are as follows:
Mydoc.xml
<Data> <VehicleData> <VehicleRecord> <id>1001</id> <make >Ford</make> <model>Escort</model> <year>1984</year> </ve hiclerecord> <VehicleRecord> <id>1002</id> <MAKE>TOYOTA</MAKE&G T <model>Tercel</model> <year>1996</year> </VehicleRecord> <vehicl erecord> <id>1003</id> <make>Mazda</make> <model>GLC< /model> <year>1985</year> </VehicleRecord> </VehicleData> <driverda ta> <DriverRecord> <id>1</id> <firstname>Albert</firstname> <lastname>Einstein</lastname> </DriverRecord> <DriverRecord> & Lt;id>2</id> <firstname>Clint</firstname> <lastname>Eastwood</lastname> </DriverRecord> <DriverRecord> <id>3</id> <firstname>james< ;/firstname> <lastname>Bond</lastname> </DriverRecord> </driverdata></d Ata>
Let me explain how to merge nodes. Suppose we have two XmlDocument files docBook1 and DocBook2,
<Book>
Node. In the docBook1
<Book>
node contains
<Introduction>
<Chapter1>
<Chapter2>
.
In the docBook2
<Book>
<Chapter3>
<Chapter4>
<Chapter5>
.
The following code shows how to merge these two book nodes:
Dim MyDoc as Xmldocumentex = New Xmldocumentex () mydoc.loadxml ("<Data> <Book></Book></Data>") Mydoc.mergenode (Docbook1.selectsinglenode ("//book"), "book", "Data") Mydoc.mergenode (Docbook2.selectsinglenode ("/ /book ")," book "," Data "
The combined effect is as follows:
Mydoc.xml
<Data> <Book> <Introduction>...</Introduction> < Chapter1 >...</ chapter1> <Chapter2>...</Chapter2> <Chapter3>...</Chapter3> < chapter4>...</chapter4> <Chapter5>...</Chapter5> </Book></Data>
Here are all the source code:
Sealed public class xmldocumentex:xmldocument{public bool AddNode (XmlNode osource, String sname) {return A Ddnode (Osource, sname, NULL); public bool AddNode (XmlNode osource, String sname, String sparent) {try {if (sname!=null &&osource!= null) {//Create the new node with given name XmlNode Onewno de = createelement (sname); Copy the contents from the source node onewnode.innerxml = Osource.innerxml; If there is no parent 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/re Turn true only if all nodes are added successfully int i = 0; while (I<osourcelist.count) { if (! AddNode (Osourcelist.item (i), sname,sparent)) return false; i++; return true; The 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!=nu ll&&osource!= 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 = createelement (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, create It then add it as a child node of the parent node Thenode = Thepa Rent. selectSingleNode (sname); if (thenode==null) {Thenode = createelement (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; The catch (Exception) {} is return false; }}