13.07.2015 Views

Applied XML Programming for Microsoft .NET.pdf - Csbdu.in

Applied XML Programming for Microsoft .NET.pdf - Csbdu.in

Applied XML Programming for Microsoft .NET.pdf - Csbdu.in

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

The <strong>in</strong>spiration <strong>for</strong> design<strong>in</strong>g such a read/write stream<strong>in</strong>g parser is database servercursors. With database server cursors, you visit records one after the next and, ifneeded, can apply changes on the fly. Database changes are immediately effective,and actually the canvas on which your code operates is simply the database table. Thesame model can be arranged to work with <strong>XML</strong> documents.You will use a normal <strong>XML</strong> (validat<strong>in</strong>g) reader to visit the nodes <strong>in</strong> sequence. Whileread<strong>in</strong>g, however, you are given the opportunity to change attribute values and nodecontents. Unlike the <strong>XML</strong> DOM, changes will have immediate effect. How can youobta<strong>in</strong> these results? The idea is to use an <strong>XML</strong> writer on top of the reader.You use the reader to read each node <strong>in</strong> the source document and an underly<strong>in</strong>g writerto create a hidden copy of it. In the copy, you can add some new nodes and ignore oredit some others. When you have f<strong>in</strong>ished, you simply replace the old document withthe new one. You can decide to write the copy <strong>in</strong> memory or flush it <strong>in</strong> a temporarymedium. The latter approach makes better use of the system's memory and saves youfrom possible troubles with the application's security level and zones. (For example,partially trusted W<strong>in</strong>dows Forms applications and default runn<strong>in</strong>g ASP.<strong>NET</strong> applicationscan't create or edit disk files.)Built-In Support <strong>for</strong> Read/Write OperationsWhen I first began th<strong>in</strong>k<strong>in</strong>g about this lightweight <strong>XML</strong> DOM component, one of keypo<strong>in</strong>ts I identified was an efficient way to copy (<strong>in</strong> bulk) blocks of nodes from the readonlystream to the write stream. Luckily enough, two somewhat underappreciatedXmlTextWriter methods just happen to cover this tricky but bor<strong>in</strong>g aspect of two-waystream<strong>in</strong>g: WriteAttributes and WriteNode.The WriteAttributes method reads all the attributes available on the currently selectednode <strong>in</strong> the specified reader. It then copies them as a s<strong>in</strong>gle str<strong>in</strong>g to the current outputstream. Likewise, the WriteNode method does the same <strong>for</strong> any other type of node.Note that WriteNode does noth<strong>in</strong>g if the node type is XmlNodeType.Attribute.The follow<strong>in</strong>g code shows how to use these methods to create a copy of the orig<strong>in</strong>al<strong>XML</strong> file, modified to skip some nodes. The <strong>XML</strong> tree is visited <strong>in</strong> the usual node-firstapproach us<strong>in</strong>g an <strong>XML</strong> reader. Each node is then processed and written out to theassociated <strong>XML</strong> writer accord<strong>in</strong>g to the <strong>in</strong>dex. This code scans a document and writesout every other node.XmlTextReader reader = new XmlTextReader(<strong>in</strong>putFile);XmlTextWriter writer = new XmlTextWriter(outputFile);// Configure reader and writerwriter.Formatt<strong>in</strong>g = Formatt<strong>in</strong>g.Indented;reader.MoveToContent();// Write the rootwriter.WriteStartElement(reader.LocalName);// Read and output every other node<strong>in</strong>t i=0;while(reader.Read()){149

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!