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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Readers and <strong>XML</strong> ReadersTo cap off our exam<strong>in</strong>ation of <strong>XML</strong> readers and custom readers, let's spend a fewmoments look<strong>in</strong>g at the difference between an <strong>XML</strong> reader and a generic reader <strong>for</strong> anon-<strong>XML</strong> data structure.A reader is a basic and key concept <strong>in</strong> the .<strong>NET</strong> Framework. Several different types ofreader classes do exist <strong>in</strong> the .<strong>NET</strong> Framework: b<strong>in</strong>ary readers, text readers, <strong>XML</strong>readers, and database readers, just to name a few. Of course, you can add your owndata-specific readers to the list. But that's the po<strong>in</strong>t. How would you write your newreader? The simplest answer would be, you write the reader by <strong>in</strong>herit<strong>in</strong>g from one ofthe exist<strong>in</strong>g reader classes.A more precise answer should help you identify the best reader class to start from. Thekey criterion when you're choos<strong>in</strong>g a base class is the k<strong>in</strong>d of programm<strong>in</strong>g <strong>in</strong>terfaceyou expect from the new reader. Another m<strong>in</strong>or, but not negligible, concern is whetherthe class allows <strong>for</strong> <strong>in</strong>heritance. Some reader classes are sealed and do not permit<strong>in</strong>heritance. (The data reader classes, such as SqlDataReader, belong to thiscategory.)Actually, you could build your own reader class from base classes such asB<strong>in</strong>aryReader, TextReader, and XmlReader. Typically, you choose the B<strong>in</strong>aryReaderclass if you need to manipulate primitive types <strong>in</strong> b<strong>in</strong>ary rather than text <strong>for</strong>mat. Youchoose the TextReader class whenever character <strong>in</strong>put is critical. To successfully buildon top of TextReader, the most complicated th<strong>in</strong>g you might need to do is read a l<strong>in</strong>e oftext between two successive <strong>in</strong>stances of a carriage return. You choose the XmlReaderclass as the base class if the content of the data you expose can be rendered, or atleast traversed, as <strong>XML</strong>. Because <strong>XML</strong> is a very specific flavor of text, the XmlReaderclass happens to be more powerful and richer than any other reader class. Not all data,however, maps to some reasonable extent to <strong>XML</strong>. If this is the case, simply plan abrand-new reader on top of B<strong>in</strong>aryReader or TextReader as applicable.If you just want to implement a specialized <strong>XML</strong> reader (<strong>for</strong> example, a SAX reader oran <strong>XML</strong> reader support<strong>in</strong>g a different visit<strong>in</strong>g algorithm), you might also considerstart<strong>in</strong>g from XmlTextReader, XmlNodeReader, or XmlValidat<strong>in</strong>gReader. An <strong>XML</strong>specialized reader is basically a reader designed to handle data that is natively storedas well-<strong>for</strong>med <strong>XML</strong>.ConclusionSo far, we've covered the basics of <strong>XML</strong> readers. By now, you should know how toparse an <strong>XML</strong> document irrespective of its physical location and storage medium. Youknow how to move between nodes, how to skip unneeded nodes, and how to readcontents and attributes. In short, you have gotten the gist of <strong>XML</strong> readers.The reader is a general concept that crosses the whole spectrum of .<strong>NET</strong> Frameworkfunctionalities and applies to <strong>XML</strong> as well as databases, files, and network protocols.You can also create custom <strong>XML</strong> readers to process non-<strong>XML</strong> data structures such asCSV files.We've only scratched the surface of this topic—there's a lot more to be done. Forexample, we haven't yet looked at validation, which is the topic of Chapter 3.59

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

Saved successfully!

Ooh no, something went wrong!