The. NET framework handles XML data easily (V)

Source: Internet
Author: User

Design Xmlreadwriter Class

As mentioned earlier, XmlReader and writer work independently: Reader-only, writer write only. Suppose your application manages a lengthy XML document, and the document has indeterminate data. Reader provides a good way to read the contents of the document. Writer, on the other hand, is a very useful tool for creating snippets of XML documents, but if you want it to be readable and writable, then you need to use XMLDOM. If the actual XML document is very large, then a problem arises, what is the problem? Is this XML document loaded into memory and then read and written? Let's take a look at how to build a mixed flow analyzer for analyzing large xmldom.

Like a normal read-only operation, access nodes are ordered using ordinary XmlReader. The difference is that you can use XmlWriter to change the value of the attribute and the contents of the node while you are reading it. You use reader to read each node in the source file, and writer in the background creates a copy of the node. In this copy, you can add new nodes, ignore or edit other nodes, and edit the values of the properties. When you have completed the changes, you will replace the old document with the new one.

A simple and effective way is to copy the node object into the write stream from the read-only stream, which can be used in the two methods in the XmlTextWriter class: The WriteAttributes method and the WriteNode method. The WriteAttributes method reads all valid properties for the selected node in the current reader, and then copies the attribute as a separate string into the current output stream. Similarly, the WriteNode method handles other types of nodes except the attribute nodes in a similar way. The code snippet shown in Figure 10 demonstrates how to create a copy of a source XML document using the above two methods, optionally modifying some nodes. The XML tree is accessed from the root, but only nodes of a type other than the attribute node type are exported. You can integrate reader and writer into a new class, design a new interface that allows it to read and write streams and access properties and nodes.

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

My Xmltextreadwriter class did not inherit from the XmlReader or XmlWriter class. Instead, there are two other classes, one based on the read-only stream (stream), and the other based on the write-only flow of an action class. The Xmltextreadwriter class method reads the data with the reader object and writes to the writer object. In order to adapt to different requirements, the internal reader and writer objects are exposed by read-only reader and writer attributes respectively. Figure 11 lists some of the methods for this class:

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.