在.NET Framework中轻松处理XML数据(5-1)

2016-02-19 21:16 0 1 收藏

下面是个在.NET Framework中轻松处理XML数据(5-1)教程,撑握了其技术要点,学起来就简单多了。赶紧跟着图老师小编一起来看看吧!

【 tulaoshi.com - Web开发 】

  设计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

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)

XMLTextReader reader = new XmlTextReader(inputFile);

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)

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方法提交修改,清除临时表。

来源:https://www.tulaoshi.com/n/20160219/1625741.html

延伸阅读
标签: Web开发
XmlTextWriter类 用在本节中的方法创建XML文档显然并不困难。多年以来,开发者都是通过在缓存在连接一些字符串,连接好以后再把缓存中字符串输出到文件的方式来创建XML文档。但是以这种方式创建XML文档的方法只有在你保证字符串中不存在任何细小的错误的时候才有效。.NET Framework通过用XMLwriter提供了更好的创建XML文档的方法。 X...
标签: Web开发
分析属性值 大部分情况下,属性值都是一个简单的文本字符串。然而,这并不意味着实际应用中的属性值都是字符型的。有时候,属性值是由许多种类型的数据组合而成的,例如Date或Boolean,这时,你就要用XmlConvert或System.Convevt类的方法把这些类型转换成原来的类型。XmlConvert和System.Convevt类都能实现数据类型的转换,但是XmlConv...
标签: Web开发
在.NET Framework中,XmlTextReader和XmlTextWriter类提供了对xml数据的读和写操作。在本文中,作者讲述了XML阅读器(Reader)的体系结构及它们怎样与XMLDOM 和SAX 解释器结合。作者也演示了怎么样运用阅读器分析和验证XML文档,怎么样创建格式良好的XML文档,以及怎么样用函数读/写基于Base64和BinHex编码的大型的XML文档。最后,作者讲了怎么样...
标签: Web开发
Figure 7 States for XML Writer State Description Attribute The writer enters this state when an attribute is being written Closed The Close method has been called and the writer is no longer available for writing operations Content The writer enters this state when the content of a node is being written ...
标签: Web开发
图八中代码演示了把一个string数据转换为Base64 编码的XML流。图九是输出的结果。 Figure 8 Persisting a String Array as Base64 using System; using System.Text; using System.IO; using System.XML; class MyBase64Array { public static void Main(String[] args) { string outputFileName = "test64.XML"; if...

经验教程

106

收藏

98
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部