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.

Chapter 2: <strong>XML</strong> ReadersIn the <strong>Microsoft</strong> .<strong>NET</strong> Framework, two dist<strong>in</strong>ct sets of classes provide <strong>for</strong> <strong>XML</strong>-drivenread<strong>in</strong>g and writ<strong>in</strong>g operations. These classes are known globally as <strong>XML</strong> readers andwriters. The base class <strong>for</strong> readers is XmlReader, whereas XmlWriter provides the baseprogramm<strong>in</strong>g <strong>in</strong>terface <strong>for</strong> writers. In this chapter, we'll focus on a particular type of<strong>XML</strong> readers—the <strong>XML</strong> text readers. In Chapter 3, we'll zero <strong>in</strong> on validat<strong>in</strong>g readersand then move on to <strong>XML</strong> writers <strong>in</strong> Chapter 4.The <strong>Programm<strong>in</strong>g</strong> Interface of ReadersXmlReader is an abstract class available from the System.Xml namespace. It def<strong>in</strong>esthe set of functionalities that an <strong>XML</strong> reader exposes to let developers access an <strong>XML</strong>stream <strong>in</strong> a noncached, <strong>for</strong>ward-only, read-only way.An <strong>XML</strong> reader works on a read-only stream by jump<strong>in</strong>g from one node to the next <strong>in</strong> a<strong>for</strong>ward-only direction. The <strong>XML</strong> reader ma<strong>in</strong>ta<strong>in</strong>s an <strong>in</strong>ternal po<strong>in</strong>ter to the currentnode and its attributes and text but has no notion of previous and next nodes. You can'tmodify text or attributes, and you can move only <strong>for</strong>ward from the current node. If youare visit<strong>in</strong>g attribute nodes, however, you can move back to the parent node or accessan attribute by <strong>in</strong>dex. The visit takes place <strong>in</strong> node-first order, but other visit<strong>in</strong>galgorithms can be arranged <strong>in</strong> custom reader classes. See the note on page 72 <strong>for</strong>more <strong>in</strong><strong>for</strong>mation about visit<strong>in</strong>g algorithms.The specification <strong>for</strong> the XmlReader class recommends that any derived class shouldcheck at least whether the <strong>XML</strong> source is well-<strong>for</strong>med and throw exceptions if an erroris encountered. <strong>XML</strong> exceptions are handled through the tailor-made XmlExceptionclass. The <strong>XML</strong>Reader class specification does not say anyth<strong>in</strong>g about <strong>XML</strong> validation.Throughout this chapter, you'll see that the .<strong>NET</strong> Framework provides several readerclasses with and without validation capabilities. Valid sources <strong>for</strong> an <strong>XML</strong> reader aredisk files as well as any flavor of .<strong>NET</strong> streams and text readers (<strong>for</strong> example, str<strong>in</strong>greaders).An OOP RefresherThroughout this book, I'll often use terms such as <strong>in</strong>terface and class, sometimesqualified by helper adjectives such as abstract or base. Although a full explanation ofthese terms and their related object-oriented programm<strong>in</strong>g (OOP) concepts is beyondthe scope of this book, a quick term<strong>in</strong>ology refresher will help you get to the heart ofthe <strong>XML</strong> class hierarchy <strong>in</strong> the .<strong>NET</strong> Framework.In the .<strong>NET</strong> Framework, an <strong>in</strong>terface is a conta<strong>in</strong>er <strong>for</strong> a named collection of method,property, and event def<strong>in</strong>itions referred to as a contract An <strong>in</strong>terface can be used as areference type, but it is not a creatable type. Other types can implement one or more<strong>in</strong>terfaces. In do<strong>in</strong>g so, they adhere to the <strong>in</strong>terface's contract and agree to provideactual implementation <strong>for</strong> all the methods, properties, and events <strong>in</strong> the contract.A class is a conta<strong>in</strong>er that can <strong>in</strong>clude data and function members (methods,properties, events, operators, and constructors). Classes support <strong>in</strong>heritance fromother classes as well as from <strong>in</strong>terfaces. Any class from which another class <strong>in</strong>herits iscalled a base class.An abstract class simply declares its members without provid<strong>in</strong>g any implementation.Like <strong>in</strong>terfaces, abstract classes are not creatable but can be used as reference types.An abstract class differs from an <strong>in</strong>terface <strong>in</strong> that it has a slightly richer set of <strong>in</strong>ternalmembers (constructors, constants, and operators). Members of an abstract class canbe scoped as private, public, or protected, whereas members of an <strong>in</strong>terface aremostly public. In addition, child classes can implement multiple <strong>in</strong>terfaces but can<strong>in</strong>herit from only one class.21

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

Saved successfully!

Ooh no, something went wrong!