Design XMLReadwriter class As mentioned earlier, XML Reader and Writer are independent: 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 the root 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 write.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 XI lists some methods of the 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. this new class has a Read method, it is a simple package to read method Reader's. 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).