大家想必一定都了解XML
下面是這個類中操作節點的常用方法
// create a new node in the document object from the source node
//and name it as
// the return value indicates success or failure
public bool AddNode(XmlNode oSource
// 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
// create a set of new nodes in the document object from the source node
// list and name them as
// the return value indicates success or failure
public bool AddNodes(XmlNodeList oSourceList
// 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
// merge the source node into a node named
// the node named
// the return value indicates success or failure
public bool MergeNode(XmlNode oSource
// 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
下面我們給一個增加節點的例子
docVechile
<VehicleData>
<Record>
<id>
<make>Ford</make>
<model>Escort</model>
<year>
</Record>
<Record>
<id>
<make>Toyota</make>
<model>Tercel</model>
<year>
</Record>
<Record>
<id>
<make>Mazda</make>
<model>GLC</model>
<year>
</Record>
</VehicleData>
docDriver
<DriverData>
<Record>
<id>
<firstname>Albert</firstname>
<lastname>Einstein</lastname>
</Record>
<Record>
<id>
<firstname>Clint</firstname>
<lastname>Eastwood</lastname>
</Record>
<Record>
<id>
<firstname>James</firstname>
<lastname>Bond</lastname>
</Record>
</DriverData>
下面的代碼將增加一個節點
Dim myDoc As XMLDocumentEx = New XMLDocumentEx()
myDoc
myDoc
myDoc
myDoc
<VehicleRecord>
<id>
<make>
<model>
<year>
</ Vehicle Record>
<DriverRecord>
<id>
<firstname>
<lastname>
</DriverRecord>
</Data> 我們也可是使用AddNodes方法把一個記錄集的所有記錄增加到節點上
myDoc
myDoc
myDoc
<VehicleData>
<VehicleRecord>
<id>
<make>Ford</make>
<model>Escort</model>
<year>
</VehicleRecord>
<VehicleRecord>
<id>
<make>Toyota</make>
<model>Tercel</model>
<year>
</VehicleRecord>
<VehicleRecord>
<id>
<make>Mazda</make>
<model>GLC</model>
<year>
</VehicleRecord>
</VehicleData>
<DriverData>
<DriverRecord>
<id>
<firstname>Albert</firstname>
<lastname>Einstein</lastname>
</DriverRecord>
<DriverRecord>
<id>
<firstname>Clint</firstname>
<lastname>Eastwood</lastname>
</DriverRecord>
<DriverRecord>
<id>
<firstname>James</firstname>
<lastname>Bond</lastname>
</DriverRecord>
</DriverData>
</Data>下面我介紹如何合並節點
myDoc
myDoc
myDoc
<Book>
<Introduction>
< Chapter
<Chapter
<Chapter
<Chapter
<Chapter
</Book>
</Data>下面是所有的源代碼
{
public bool AddNode(XmlNode oSource
{
return AddNode(oSource
}
public bool AddNode(XmlNode oSource
{
try
{
if(sName!=null&&oSource!= null)
{
// create the new node with given name
XmlNode oNewNode = CreateElement(sName);
// copy the contents from the source node
oNewNode
// if there is no parent node specified
// the new node as a child node of the root node
if(sParent!= null) sParent = sParent
if(sParent== null||sParent
{
DocumentElement
return true;
}
// otherwise add the new node as a child of the parent node
else
{
if (!sParent
XmlNode oParent = SelectSingleNode(sParent);
if (oParent!=null)
{
oParent
return true ;
}
}
}
}
catch (Exception)
{
// error handling code
}
return false;
}
public bool AddNodes(XmlNodeList oSourceList
{
return AddNodes(oSourceList
}
public bool AddNodes(XmlNodeList oSourceList
{
try
{
if(oSourceList!= null)
{
// call AddNode for each item in the source node list
// return true only if all nodes are added successfully
int i =
while(i<oSourceList
{
if (!AddNode(oSourceList
i++;
}
return true;
}
}
catch (Exception)
{
// error handling code
}
return false;
}
public bool MergeNode(XmlNode oSource
{
return MergeNode(oSource
}
public bool MergeNode(XmlNode oSource
{
try
{
if(sName!=null&&oSource!= null)
{
XmlNode theNode = null ;
// if there is no parent node specified
if(sParent!= null) sParent = sParent
if(sParent== null||sParent
{
// if the node with specified name does not exist
// add it as a child node of the root node
theNode = SelectSingleNode(
if (theNode==null)
{
theNode = CreateElement(sName);
DocumentElement
}
}
// if the parent node is specified
else
{
// find the parent node
if (!sParent
XmlNode theParent = SelectSingleNode(sParent);
if (theParent!=null)
{
// if the node with specified name does not exist
// it first
theNode = theParent
if(theNode==null)
{
theNode = CreateElement(sName);
theParent
}
}
}
// merge the content of the source node into
// the node with specified name
if(theNode!= null)
{
theNode
return true;
}
}
}
catch (Exception)
{
}
return false;
}
}
From:http://tw.wingwit.com/Article/program/ASP/201311/21670.html