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.

Figure 1-1: Streams can be read and written us<strong>in</strong>g made-to-measure reader and writerclasses.The base classes are TextReader, TextWriter, B<strong>in</strong>aryReader, B<strong>in</strong>aryWriter, andStream. With the exception of the b<strong>in</strong>ary classes, all of these classes are marked asabstract (MustInherit, if you speak Visual Basic) and cannot be directly <strong>in</strong>stantiated <strong>in</strong>code. You can use abstract classes to reference liv<strong>in</strong>g <strong>in</strong>stances of derived classes,however.In the .<strong>NET</strong> Framework, base reader and writer classes f<strong>in</strong>d a number of concreteimplementations, <strong>in</strong>clud<strong>in</strong>g StreamReader and Str<strong>in</strong>gReader and their writ<strong>in</strong>gcounterparts. By design, reader and writer classes work on top of .<strong>NET</strong> streams andprovide programmers with a customized user <strong>in</strong>terface able to handle a particular typeof underly<strong>in</strong>g data or file <strong>for</strong>mat. Although each specific reader or writer class is tailormade<strong>for</strong> the content of a given type of stream, they share a common set of methodsand properties that def<strong>in</strong>es the official .<strong>NET</strong> <strong>in</strong>terface <strong>for</strong> read<strong>in</strong>g and writ<strong>in</strong>g data.The Cursor-Like ApproachA reader works <strong>in</strong> much the same way as a client-side database cursor. The underly<strong>in</strong>gstream is seen as a logical sequence of units of <strong>in</strong><strong>for</strong>mation whose size and layoutdepend on the particular reader. Like a cursor, the reader moves through the data <strong>in</strong> aread-only, <strong>for</strong>ward-only way. Normally, a reader is not expected to cache any<strong>in</strong><strong>for</strong>mation, but this is only common practice, rather than a strict requirement <strong>for</strong> allstandard .<strong>NET</strong> readers.ADO.<strong>NET</strong> data reader classes (<strong>for</strong> example, SqlDataReader) are simply .<strong>NET</strong> readersthat move from one record to the next and expose the contents of the current recordthrough a tailor-made <strong>in</strong>terface. The unit of <strong>in</strong><strong>for</strong>mation read at every step is thedatabase row. Similarly, a reader work<strong>in</strong>g on a disk file stream would consider as itsown atomic unit of <strong>in</strong><strong>for</strong>mation the s<strong>in</strong>gle byte, whereas a text reader would perhapsspecialize <strong>in</strong> extract<strong>in</strong>g one row of text at a time.<strong>XML</strong> readers are simply another, very peculiar, type of .<strong>NET</strong> reader. The class parsesthe contents of an <strong>XML</strong> file, mov<strong>in</strong>g from one node to the next. In this case, the f<strong>in</strong>ergra<strong>in</strong> of the <strong>in</strong><strong>for</strong>mation processed is represented by the <strong>XML</strong> node—be it an element,an attribute, a comment, or a process<strong>in</strong>g <strong>in</strong>struction.<strong>XML</strong> ReadersAn <strong>XML</strong> reader makes externally available a programm<strong>in</strong>g <strong>in</strong>terface through whichcallers can connect and pull out all the data they need. This is <strong>in</strong> no way different fromwhat happens when you connect to a database and fetch data. The database serverreturns a reference to an <strong>in</strong>ternal object—the cursor—which manages all the queryresults and makes them available on demand. This statement applies regardless of thefact that the database world might provide several flavors of cursors—client, scrollable,server-side, and so on.With <strong>XML</strong> readers, client applications are returned a reference to an <strong>in</strong>stance of thereader class, which abstracts the underly<strong>in</strong>g data stream. Methods on the reader classallow you to scroll <strong>for</strong>ward through the contents, mov<strong>in</strong>g from node to node rather thanfrom byte to byte or from record to record. When viewed from the perspective ofreaders, an <strong>XML</strong> document ceases to be a tagged text file and becomes a serializedcollection of nodes. Such a cursor model is specific to the .<strong>NET</strong> plat<strong>for</strong>m, and to date,you will not f<strong>in</strong>d a similar programm<strong>in</strong>g API available <strong>for</strong> other plat<strong>for</strong>ms, <strong>in</strong>clud<strong>in</strong>g<strong>Microsoft</strong> W<strong>in</strong>32.15

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

Saved successfully!

Ooh no, something went wrong!