在.NET中操作XmlDocument (轉)

worldblog發表於2008-01-31
在.NET中操作XmlDocument (轉)[@more@]

大家想必一定都瞭解,利用XML技術來資料和文件是一件很容易的事情, 在它的名稱空間System.Xml 就提供了一種可以很方便的操作xml的類XmlDocument,它使用起來非常容易,XmlDocument 其實就是一個簡單的樹。下面詳細的介紹XmlDocument 的使用方法。

下面是這個類中操作節點的常用方法。

// create a new node in the document from the 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

1001 Ford Ert 1984 1002 Toyota Tercel 1996 1003 Mazda GLC 1985


doc.xml

1 Albert Einstein 2 Clint Eastwood 3 James Bond


下面的程式碼將增加一個節點:


Dim myDoc As XMLDocumentEx = New XMLDocumentEx() myDoc.LoadXml("") myDoc.AddNode(docVehicle.SingleNode("//Record"), "VehicleRecord", "Data") myDoc.AddNode(docDriver.SelectSingleNode("//Record"), "DriverRecord", "Data")


myDoc.xml

... ... ... ... Vehicle Record> ... ... ...


 


我們也可是使用AddNodes方法把一個記錄集的所有記錄增加到節點上:


Dim myDoc As XMLDocumentEx = New XMLDocumentEx() myDoc.LoadXml(" ") myDoc.AddNodes(docVehicle.SelectNodes("//Record"), "VehicleRecord", " Vehicle Data") myDoc.AddNodes(docDriver.SelectNodes("//Record"), "DriverRecord", "DriverData")


結果如下:


myDoc.xml

1001 Ford Escort 1984 1002 Toyota Tercel 1996 1003 Mazda GLC 1985 1 Albert Einstein 2 Clint Eastwood 3 James Bond


下面我介紹如何合併節點。假設我們有兩個XmlDocumentdocBook1和docBook2,


這兩個文件都包含 節點.  在docBook1 中的這個 節點 包含


, , and .  


docBook2中的這個 節點 包含 , , and .  


下面的程式碼演示如何合併這兩個book節點:


Dim myDoc As XMLDocumentEx = New XMLDocumentEx() myDoc.LoadXml(" ") myDoc.MergeNode(docBook1.SelectSingleNode("//Book"), "Book", "Data ") myDoc.MergeNode(docBook2.SelectSingleNode("//Book"), "Book", "Data")


合併後的效果如下:


myDoc.xml

... < Chapter1 >... ... ... ... ...


下面是所有的:


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&&oSource!= 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 parent node specified, then add // the new node as a child node of the 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 if all nodes are added succesully int i = 0; while(i


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752043/viewspace-998866/,如需轉載,請註明出處,否則將追究法律責任。

相關文章