?? As mentioned earlier, XML reader and writer work independently: Reader is read only and writer writes only. Let's say that you use the program to govern lengthy XML documents that have constant data. Reader provides a good way to read the contents of the document. On the other hand, writer is a useful tool for creating fragments of XML documents, but if you want it to read and write, then you need to use XMLDOM. If the actual XML document is very ambitious, it will present a title, what title? Would you like to load this XML document into memory, then read and write it backwards? Let's take a look at how to build a hybrid stream analyzer for analyzing large xmldom.
?? As with the general read-only control, use normal XML reader to go to the sequential call node. The difference is that while you are reading, you can use XML writer to transform attribute values and the contents of nodes. 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, neglect or compile some other nodes, and compile the values of the attributes. When you're done with the fix, you'll replace the old document with a new one.
?? A simple and effective measure is to copy a node object from a read-only stream into the write stream, which can be used with two methods in the XmlTextWriter class: The WriteAttributes method and the WriteNode method. The WriteAttributes method reads all the valid properties of 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, other than attribute nodes, in a similar way. The code snippet shown in Figure 10 shows how to create a copy of the source XML document using the two methods described above, and selectively fix some nodes. XML trees are visited from the root of the tree, but only the nodes of the type other than the attribute node type are exported. You can integrate reader and writer into a new class and design a new interface that allows it to read and write streams and call properties and nodes.
Figure Ten Using the WriteNode Method
XmlTextReader reader = new XmlTextReader (Inputfile);
XmlTextWriter writer = new XmlTextWriter (outputFile);
Configuring Reader and writer
Writer. formatting = formatting.indented;
Reader. MoveToContent ();
Write root node
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 does not persist 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 control class. The method of the Xmltextreadwriter class reads the data with the reader object and writes it into the writer object. In order to adapt to different needs, the internal reader and writer objects are resolved through read-only reader and writer attributes openly. Figure 11 lists some of the methods of the class:
Figure One 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 is 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, which is a brief encapsulation of the Read method of reader. In addition, it supplies the writerstartdocument and WriteEndDocument methods. They distinguish the initialization/release (finalize) of the internal reader and writer objects, and also handle all I/O control. As we iterate through the nodes, we can directly modify the nodes. For performance reasons, it is necessary to first declare the property with the Addattributechange method. All corrections to a node's properties are stored in a temporary table, and finally, the temporary table is cleared by invoking the WriteAttribute method to commit the fixup.
The above is the easy processing of XML data (5-1) in the. NET framework, and more about topic.alibabacloud.com (www.php.cn)!
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.