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.

managed application, you must first create a .<strong>NET</strong> wrapper class that exposes all thepublic methods and properties found <strong>in</strong> the component's type library. <strong>Microsoft</strong> VisualStudio .<strong>NET</strong>, <strong>for</strong> example, creates such a class on the fly, immediately after add<strong>in</strong>g theproper library reference to the current project.Dur<strong>in</strong>g the process, the <strong>in</strong>volved types are converted from COM types and adapted tofit <strong>in</strong>to the .<strong>NET</strong> Framework type system. After the importation is complete, the orig<strong>in</strong>alCOM object is ready <strong>for</strong> use <strong>in</strong> the .<strong>NET</strong> Framework, and more importantly, it haspreserved the orig<strong>in</strong>al <strong>in</strong>terface while add<strong>in</strong>g some .<strong>NET</strong> Framework-specific memberssuch as ToStr<strong>in</strong>g and GetType. In the end, <strong>for</strong> a <strong>Microsoft</strong> Visual Basic 6.0 programmerwho happens to use Visual Basic .<strong>NET</strong>, the code to be written is nearly identical.NoteTo generate a .<strong>NET</strong> wrapper class <strong>for</strong> a COM object, you can alsouse the tlbimp.exe utility from the command l<strong>in</strong>e. This utility givesyou full control over the entire process, and by us<strong>in</strong>g command-l<strong>in</strong>eswitches, you can <strong>in</strong>tervene <strong>in</strong> many useful areas, <strong>in</strong>clud<strong>in</strong>g the(strong) name of the assembly and the wrapp<strong>in</strong>g namespace.Although import<strong>in</strong>g MS<strong>XML</strong> functionality <strong>in</strong>to a .<strong>NET</strong> application is straight<strong>for</strong>ward, youmust have a good reason <strong>for</strong> do<strong>in</strong>g so. Jump<strong>in</strong>g cont<strong>in</strong>uously <strong>in</strong> and out of the .<strong>NET</strong>common language runtime (CLR) can result <strong>in</strong> a per<strong>for</strong>mance hit—not to mention thefact that you end up us<strong>in</strong>g a programm<strong>in</strong>g model that, although perfectly functional, isnot the best suited <strong>for</strong> the surround<strong>in</strong>g environment.The .<strong>NET</strong> Framework <strong>XML</strong> APIThe essence of <strong>XML</strong> <strong>in</strong> the .<strong>NET</strong> Framework is found <strong>in</strong> two abstract classes—XmlReader and XmlWriter. These classes are at the core of all other .<strong>NET</strong> Framework<strong>XML</strong> classes, <strong>in</strong>clud<strong>in</strong>g the <strong>XML</strong>DOM classes, and are used extensively by varioussubsystems to parse or generate <strong>XML</strong> text. For example, ADO.<strong>NET</strong> data adaptersretrieve the data to store <strong>in</strong> a DataSet object us<strong>in</strong>g a database reader, and the DataSetobject serializes its contents to the DiffGram <strong>for</strong>mat us<strong>in</strong>g an XmlTextWriter object,which derives from XmlWriter.<strong>XML</strong> readers and writers constitute the primitive I/O functions <strong>for</strong> <strong>XML</strong> documents andare used to build more sophisticated functionalities. So overall, you have two possibleapproaches when it comes to process<strong>in</strong>g <strong>XML</strong> data. You can use any of the specializedclasses built on top of XmlReader and XmlWriter as well as document classes thatexpose the contents through the well-known and classic <strong>XML</strong>DOM.The direct use of readers represents a stream-based, but fast and stateless, approachto <strong>XML</strong> pars<strong>in</strong>g. The use of <strong>XML</strong>DOM classes (<strong>for</strong> example, XmlDocument) representsthe traditional <strong>XML</strong>DOM pars<strong>in</strong>g model. Readers are representative of a pull model, asopposed to the SAX parser's typical push model. You can certa<strong>in</strong>ly build a push modelatop a pull model-based API. Un<strong>for</strong>tunately, the reverse is never true, and that's whythere is no SAX support <strong>in</strong> the .<strong>NET</strong> Framework. (In Chapter 2, you'll learn the basics ofimplement<strong>in</strong>g a SAX parser us<strong>in</strong>g .<strong>NET</strong> Framework <strong>XML</strong> readers.)The <strong>XML</strong> API <strong>for</strong> the .<strong>NET</strong> Framework comprises the follow<strong>in</strong>g set of functionalities:• <strong>XML</strong> readers• <strong>XML</strong> writers• <strong>XML</strong> document classesAll of these functionalities must overcome the rather subtle problem of type mapp<strong>in</strong>g.The .<strong>NET</strong> Framework <strong>XML</strong> type system has several th<strong>in</strong>gs <strong>in</strong> common with the XSDSchema type system, and ad hoc conversion classes <strong>in</strong> the .<strong>NET</strong> Framework provide<strong>for</strong> applicable trans<strong>for</strong>mations.13

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

Saved successfully!

Ooh no, something went wrong!