电脑爱好者,提供IT资讯信息及各类编程知识文章介绍,欢迎大家来本站学习电脑知识。 最近更新 | 联系我们 RSS订阅本站最新文章
电脑爱好者
站内搜索: 
当前位置:首页>> XML专区>>在.NET Framework中轻松处理XML数据(五):

在.NET Framework中轻松处理XML数据(五)

来源:远方网络 | 2006-2-28 | (有3186人读过)

设计XmlReadWriter类
如前面所说,XML reader和Writer是各自独立工作的:reader只读,writer只写。假设你的应用程序要管理冗长的XML文档,且该文档有不确定的数据。Reader提供了一个很好的方法去读该文档的内容。另一方面,Writer是一个非常有用的用于创建XML文档片断工具,但是如果你想要它即能读,又能写,那么你就要用XMLDOM了。如果实际的XML文档非常庞大,又会出现了一个问题,什么问题呢?是不是把这个XML文档全部加载到内存中,然后进行读和写呢?让我们先看一下怎么样建立一个混合的流分析器用于分析大型的XMLDOM。

像一般的只读操作一样,用普通的XML reader去顺序的访问节点。不同的是,在读的同时你可以用XML writer改变属性值以及节点的内容。你用reader去读源文件中的每个节点,后台的writer创建该节点的一个拷贝。在这个拷贝中,你可以增加一些新的节点,忽略或者编辑其它的一些节点,还可以编辑属性的值。当你完成修改后,你就用新的文档替换旧的文档。

一个简单有效的办法是从只读流中拷贝节点对象到write流中,这种方法可以用XmlTextWriter类中的两个方法:WriteAttributes方法和WriteNode方法。 WriteAttributes方法读取当前reader中选中的节点的所有有效的属性,然后把属性当作一个单独的string拷贝到当前的输出流中。同样的,WriteNode方法用类似的方法处理除属性节点外的其它类型的节点。图十所示的代码片断演示了怎么用上述的两个方法创建一个源XML文档的拷贝,有选择的修改某些节点。XML树从树根开始被访问,但只输出了除属性节点类型以外的其它类型的节点。你可以把Reader和Writer整合在一个新的类中,设计一个新的接口,使它能读写流及访问属性和节点。

Figure 10 Using the WriteNode Method

XmlTextReader reader = new XmlTextReader(inputFile);

XmlTextWriter writer = new XmlTextWriter(outputFile);



// 配置 reader 和 writer

writer.Formatting = Formatting.Indented;

reader.MoveToContent();



// Write根节点

writer.WriteStartElement(reader.LocalName);



// Read and output every other node

int i=0;

while(reader.Read())

{

if (i % 2)

writer.WriteNode(reader, false);

i++;

}



// Close the root

writer.WriteEndElement();



// Close reader and writer

writer.Close();

reader.Close();

我的XmlTextReadWriter类并没有从XmlReader或者XmlWriter类中继承。取而代之的是另外两个类,一个是基于只读流(stream)的操作类,另一个是基于只写流的操作类。XmlTextReadWriter类的方法用Reader对象读数据,写入到Writer对象。为了适应不同的需求,内部的Reader和Writer 对象分别通过只读的Reader和Writer属性公开。图十一列出了该类的一些方法:

Figure 11 XmlTextReadWriter Class Methods

Method
Description

AddAttributeChange
Caches all the information needed to perform a change on a node attribute. All the changes cached through this method are processed during a successive call to WriteAttributes.

Read
Simple wrapper around the internal reader's Read method.

WriteAttributes
Specialized version of the writer's WriteAttributes method, writes out all the attributes for the given node, taking into account all the changes cached through the AddAttributeChange method.

WriteEndDocument
Terminates the current document in the writer and closes both the reader and the writer.

WriteStartDocument
Prepares the internal writer to output the document and add a default comment text and the standard XML prolog.


这个新类有一个Read方法,它是对Reader的read方法的一个简单的封装。另外,它提供了WriterStartDocument和WriteEndDocument方法。它们分别初始化/释放(finalize)了内部Reader和writer对象,还处理所有I/O操作。在循环读节点的同时,我们就可以直接的修改节点。出于性能的原因,要修改属性必须先用AddAttributeChange方法声明。对一个节点的属性所作的所有修改都会存放在一个临时的表中,最后,通过调用WriteAttribute方法提交修改,清除临时表。

图十二所示的代码演示了客户端用XmlTextReadWriter类在读操作的同时修改属性值的优势。在本期的msdn中提供了XmlTextReadWriter类的C#和VB源代码下载(见本文开头提供的链接)。
Figure 12 Changing Attribute Values

private void ApplyChanges(string nodeName, string attribName,

string oldVal, string newVal)

{

XmlTextReadWriter rw = new XmlTextReadWriter(InputFileName.Text,

OutputFileName.Text);

rw.WriteStartDocument(true, CommentText.Text);



// 手工修改根节点

rw.Writer.WriteStartElement(rw.Reader.LocalName);



// 开始修改属性

// (可以修改更多节点的属性)

rw.AddAttributeChange(nodeName, attribName, oldVal, newVal);



// 循环处理文档

while(rw.Read())

{

switch(rw.NodeType)

{

case XmlNodeType.Element:

rw.Writer.WriteStartElement(rw.Reader.LocalName);

if (nodeName == rw.Reader.LocalName)

// 修改属性

rw.WriteAttributes(nodeName);

else

// deep copy

rw.Writer.WriteAttributes(rw.Reader, false);



if (rw.Reader.IsEmptyElement)

rw.Writer.WriteEndElement();

break;

}

}



// Close the root tag

rw.Writer.WriteEndElement();



// Close the document and any internal resources

rw.WriteEndDocument();

}



XmlTextReadWriter类不仅可以读XML文档,也可以写XML文档。你可以它来读XML文档的内容,如果需要,你还可以用它来做一些基本的更新操作。基本的更新操作在这里是指修改某个已存在的属性的值或者某个节点的内容,又或者是增加一个新的属性或节点。对于更复杂的操作,最好还是用XMLDOM分析器。

总结

Reader和Writer是.NET Framework中处理XML数据的根本。它们提供了对所有XML数据访问功能的原始的API。Reader像一个新的分析器类,它即有XMLDOM的强大,又有SAX的快速简单。Writer为简单的创建XML文档而设计。虽然Reader和Writer都是.NET Framework中的一小块,但是它们是相互独立的API。在本文中,我们只讨论了怎么样用Reader和Writer完成一些主要的工作, 介绍了验证分析器的原理机制,并把Reader和writer整合在一个单独的类中。上述所有的这些类都是轻量级的,类似于游标式的XMLDOM分析器。 (chyich翻译/ASPCool)
XML专区热门文章排行
网站赞助商
购买此位置

 

关于我们 | 网站地图 | 文档一览 | 友情链接| 联系我们

Copyright © 2003-2024 电脑爱好者 版权所有 备案号:鲁ICP备09059398号