选择显示字体大小

.net framework轻松处理xml数据(五)

设计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分析器。


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons