大家想必一定都了解XML,利用XML技术来存储数据和文档是一件很容易的事情,.NET Framework 在它的命名空间System.Xml 就提供了一种可以很方便的操作xml的类XmlDocument,它使用起来非常容易,XmlDocument 其实就是一个简单的树。下面详细的介绍XmlDocument 的使用方法。
下面是这个类中操作节点的常用方法。
// 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" will be 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);
下面我们给一个增加节点的例子
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>
下面的代码将增加一个节点:
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> 我们也可是使用AddNodes方法把一个记录集的所有记录增加到节点上: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")结果如下:myDoc.xml<Data>
<VehicleData>
<VehicleRecord>
<id>1001</id>
<make>Ford</make>
<model>Escort</model>
<year>1984</year>
</VehicleRecord>
<VehicleRecord>
<id>1002</id>
<make>Toyota</make>
<model>Tercel</model>
<year>1996</year>
</VehicleRecord>
<VehicleRecord>
<id>1003</id>
<make>Mazda</make>
<model>GLC</model>
<year>1985</year>
</VehicleRecord>
</VehicleData>
<DriverData>
<DriverRecord>
<id>1</id>
<firstname>Albert</firstname>
<lastname>Einstein</lastname>
</DriverRecord>
<DriverRecord>
&nbs