Easily handle XML data in .NET Framework (5)

zhaozj2021-02-17  58

Design XMLReadwriter class

As mentioned earlier, XML Reader and Writer are their own independent work: Reader read-only, Writer is only written. Suppose your application wants to manage lengthy XML documents, and this document has undetermined data. Reader provides a good way to read the contents of this document. On the other hand, Writer is a very useful for creating an XML document segment tool, but if you want it to read, you can write, then you have to use Xmldom. If the actual XML document is very large, there will be a problem, what is the problem? Is it all loaded into memory in memory, then read and write? Let's take a look at how to build a mixed stream analyzer is used to analyze large XMLDoms.

Like a general read-only operation, use a normal XML Reader to order access nodes. Different, you can use XML Writer to change the property value and the content of the node while reading. You use Reader to read each node in the source file, and the Writer in the background creates a copy of the node. In this copy, you can add some new nodes, ignore or edit some of the other nodes, and edit the value of the properties. When you complete your modification, you will replace the old document with a new document.

A simple and effective approach is to copy the node object from the read-only stream to the WRITE stream, which can use two methods in the XMLTextWriter class: WriteAttributes method and WriteNode method. The WriteAttributes method reads all the valid properties of the nodes selected in the current Reader and then copies the properties as a separate String copy to the current output stream. Similarly, the WriteNode method handles other types of nodes except attribute nodes in a similar approach. The code snippet shown in Figure 10 demonstrates how to create a copy of the source XML document with the two methods described above, select some nodes. The XML tree begins to be accessed from the tree root, but only outputs other types of nodes other than the attribute node type. You can integrate Reader and Writer in a new class to design a new interface to read the write flow and access properties and nodes.

Figure 10 Using the Writenode Method

XmlTextReader Reader = New XMLTextReader (InputFile);

XmlTextWriter Writer = new xmlTextWriter (OutputFile);

// Configure Reader and Writer

Writer.formatting = formatting.indented;

Reader.MoveTocontent ();

// Write root node

Writer.writestartElement (Reader.LocalName);

// r r o u 中 n

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

My XMLTextReadwriter class does not inherit from the XMLReader or XMLWRITER class. Instead, two other classes are replaced, one is based on read-only stream operation, and the other is based on a write-flow operation. The XMLTextReadwriter class method reads the data with the Reader object, writes to the Writer object. In order to adapt to different needs, the internal Reader and Writer objects are exposed by read-only Reader and Writer attributes. Figure 11 lists some methods of this class: Figure 11 xmlTextReadwriter Class Methods

Method

Description

AddAttributeChange

Caches All The Information NEDED TO Perform A Change ON A Node Attribute. All The Changes Cached THIS Method Area Processed During a success

Reta

SIMPLE Wrapper Around The Internal Reader's Read Method.

WriteaTributes

.

WriteEndDocument

Terminates The Current Document In The Writer and Closes Both The Reader and The Writer.

WritestartDocument

................

This new class has an read method that is a simple package for Reader's Read method. In addition, it provides the WriterstartDocument and WriteEndDocument methods. They initialize / release (Finalize) the internal Reader and Writer objects, and processes all I / O operations. While cycling read nodes, we can modify the node directly. For performance, to modify the properties must be declared using the AddAttributeChange method. All modifications made to the properties of a node are stored in a temporary table, and finally, clear the temporary table by calling the WRITEATTRIBUTE method.

The code shown in Figure 12 demonstrates the ability to modify attribute values ​​while using the XMLTextReadwriter class. The C # and VB source code downloads for the XMLTextReadwriter class are available in this period's MSDN (links provided in this article). 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);

// Handmade the root node

rw.writer.writestartElement (rw.reader.localname); // Start modifying properties

/ / (You can modify the properties of more nodes)

RW.AddattributeChange (Nodename, AttribName, OldVal, NewVal);

// loop processing document

While (rw.read ())

{

Switch (rw.nodettype)

{

Case XMLNodetype.efficient:

Rw.writer.writestartElement (rw.reader.localname);

IF (nodename == rw.reader.localname)

// Modify the property

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

}

The XMLTextReadwriter class can not only read an XML document, but also write an XML document. You can read the content of the XML document, if you need it, you can use it to do some basic update operations. Basic update operations here refer to modifying the value of an existing property or a node, or add a new attribute or node. For more complex operations, it is best to use an XMLDOM analyzer.

to sum up

Reader

with

Writer

Yes

.NET Framework

Medium treatment

Xml

The foundation of the data. They offer all

Xml

Original

API

.

Reader

Like a new analyzer class, it has

Xmldom

Powerful,

SAX

Quick and simple.

Writer

Simple creation

Xml

Document design. although

Reader

with

Writer

All

.NET Framework

A small piece of small block, but they are independent of each other

API

. In this article, we only discuss how

Reader

with

Writer

Complete some main work

,

Introduced the principle mechanism of the verification analyzer, and

Reader

with

Writer

Integrate in a separate class. All of these classes are lightweight, similar to the vernier

Xmldom

Analyzer.

转载请注明原文地址:https://www.9cbs.com/read-29319.html

New Post(0)