分享
 
 
 

在.NET中操作XmlDocument

王朝c#·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

大家想必一定都了解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>

<id>2</id>

<firstname>Clint</firstname>

<lastname>Eastwood</lastname>

</DriverRecord>

<DriverRecord>

<id>3</id>

<firstname>James</firstname>

<lastname>Bond</lastname>

</DriverRecord>

</DriverData>

</Data>

下面我介绍如何合并节点。假设我们有两个XmlDocument文件docBook1和docBook2,

这两个文档都包含 <Book> 节点. 在docBook1 中的这个 <Book> 节点 包含

<Introduction>, <Chapter1>, and <Chapter2>.

在docBook2中的这个 <Book> 节点 包含 <Chapter3>, <Chapter4>, and <Chapter5>.

下面的代码演示如何合并这两个book节点:

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")

合并后的效果如下:

myDoc.xml

<Data>

<Book>

<Introduction>...</Introduction>

< Chapter1 >...</Chapter1>

<Chapter2>...</Chapter2>

<Chapter3>...</Chapter3>

<Chapter4>...</Chapter4>

<Chapter5>...</Chapter5>

</Book>

</Data>

下面是所有的源代码:

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 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 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;

}

}

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&&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 first, then add it as a child node of the parent node

theNode = theParent.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;

}

}

}

catch (Exception)

{

}

return false;

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有