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.

End ClassByVal type As XmlNodeType)After the two classes have been compiled <strong>in</strong>to an assembly, a client SAX applicationcan simply reference and <strong>in</strong>stantiate the parser and the content handler class. Theworld's simplest content handler class is shown here:Public Class MyContentHandlerInherits SaxContentHandlerPublic Overrides Sub Process(_ByVal name As Str<strong>in</strong>g, _ByVal value As Str<strong>in</strong>g, _ByVal type As XmlNodeType)If type = XmlNodeType.Element ThenMsgBox(name)End IfEnd SubEnd ClassThe SAX application <strong>in</strong>itializes the parser as follows:Dim saxParser As New SaxParser()Dim myHandler As New MyContentHandler()saxParser.ContentHandler = myHandlersaxParser.Parse(file)Of course, the parser discussed here is fairly m<strong>in</strong>imal, but the design guidel<strong>in</strong>es areconcrete and effective. As an aside, consider the fact that <strong>in</strong> the client application, thecontent handler class and the <strong>for</strong>m are different classes, which makes updat<strong>in</strong>g theuser <strong>in</strong>terface from the content handler class a bit complicated.NoteThe full source code discussed here is provided <strong>in</strong> this book'ssample files. The application is named SaxParser.Pars<strong>in</strong>g <strong>XML</strong> FragmentsThe XmlTextReader class provides the basic set of functionalities to process any <strong>XML</strong>data com<strong>in</strong>g from a disk file, a stream, or a URL. This k<strong>in</strong>d of reader works sequentially,read<strong>in</strong>g one node after the next, and does not deliberately provide any ad hoc searchfunction to parse only a particular subtree.In the .<strong>NET</strong> Framework, to process only fragments of <strong>XML</strong> data, excerpted from avariety of sources, you can take one of two routes. You can <strong>in</strong>itialize the text readerwith the <strong>XML</strong> str<strong>in</strong>g that represents the fragment, or you can use another, morespecific, reader class—the XmlNodeReader class.The XmlNodeReader class works on the subtree rooted <strong>in</strong> the XmlNode object passedto the class constructor. A liv<strong>in</strong>g <strong>in</strong>stance of an XmlNode object is not someth<strong>in</strong>g youcan obta<strong>in</strong> through a text reader, however. Only the .<strong>NET</strong> <strong>XML</strong> DOM parser can createand return an XmlNode object. We'll exam<strong>in</strong>e the details of the XmlNodeReader class<strong>in</strong> Chapter 5, along with the .<strong>NET</strong> <strong>XML</strong> DOM parser.41

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

Saved successfully!

Ooh no, something went wrong!