Readers and XML Readers

To cap off our examination of XML readers and custom readers, let’s spend a few moments looking at the difference between an XML reader and a generic reader for a non-XML data structure.

A reader is a basic and key concept in the .NET Framework. Several different types of reader classes do exist in the .NET Framework: binary readers, text readers, XML readers, and database readers, just to name a few. Of course, you can add your own data-specific readers to the list. But that’s the point. How would you write your new reader? The simplest answer would be, you write the reader by inheriting from one of the existing reader classes.

A more precise answer should help you identify the best reader class to start from. The key criterion when you’re choosing a base class is the kind of programming interface you expect from the new reader. Another minor, but not negligible, concern is whether the class allows for inheritance. Some reader classes are sealed and do not permit inheritance. (The data reader classes, such as SqlDataReader, belong to this category.)

Actually, you could build your own reader class from base classes such as BinaryReader, TextReader, and XmlReader. Typically, you choose the Binary­Reader class if you need to manipulate primitive types in binary rather than text format. You choose the TextReader class whenever character input is critical. To successfully build on top of TextReader, the most complicated thing you might need to do is read a line of text between two successive instances of a carriage return. You choose the XmlReader class as the base class if the content of the data you expose can be rendered, or at least traversed, as XML. Because XML is a very specific flavor of text, the XmlReader class happens to be more powerful and richer than any other reader class. Not all data, however, maps to some reasonable extent to XML. If this is the case, simply plan a brand-new reader on top of BinaryReader or TextReader as applicable.

If you just want to implement a specialized XML reader (for example, a SAX reader or an XML reader supporting a different visiting algorithm), you might also consider starting from XmlTextReader, XmlNodeReader, or Xml­ValidatingReader. An XML specialized reader is basically a reader designed to handle data that is natively stored as well-formed XML.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.205.223