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.

You can specify other sett<strong>in</strong>gs <strong>for</strong> the parser context us<strong>in</strong>g the properties of theXmlParserContext class, <strong>in</strong>clud<strong>in</strong>g Encod<strong>in</strong>g, BaseURI, and DocTypeName. Inparticular, BaseURI is especially useful because it <strong>in</strong>dicates the location from which thefragment was loaded.Writ<strong>in</strong>g a Custom <strong>XML</strong> ReaderWe have one more topic to consider on the subject of <strong>XML</strong> readers, which opens up awhole new world of opportunities: creat<strong>in</strong>g customized <strong>XML</strong> readers. An <strong>XML</strong> readerclass is merely a programm<strong>in</strong>g <strong>in</strong>terface <strong>for</strong> read<strong>in</strong>g data that appears to be <strong>XML</strong>. TheXmlTextReader class represents the simplest and the fastest of all possible <strong>XML</strong>readers but—and this is what really matters—it is just one reader. Its <strong>in</strong>herent simplicityand effectiveness stems from two key po<strong>in</strong>ts. First, the class operates as a read-only,<strong>for</strong>ward-only, nonvalidat<strong>in</strong>g parser. Second, the class is assumed to work on native<strong>XML</strong> data. It has no need, and no subsequent overhead, to map <strong>in</strong>put data <strong>in</strong>ternally to<strong>XML</strong> data structures.Virtually any data can be read, traversed, and queried as <strong>XML</strong> as long as a tailor-madepiece of code takes care of mapp<strong>in</strong>g that data to an <strong>XML</strong> Schema. This mapp<strong>in</strong>g codecan then be buried <strong>in</strong> a method that simply returns one of the standard reader objectsor creates a custom <strong>XML</strong> reader class.NoteWhat's the advantage of expos<strong>in</strong>g data through <strong>XML</strong>? <strong>XML</strong> providesa k<strong>in</strong>d of universal model <strong>for</strong> def<strong>in</strong><strong>in</strong>g a set of <strong>in</strong><strong>for</strong>mation (<strong>in</strong>foset),the type and layout of constituent items (<strong>XML</strong> Schema), and thequery commands (XPath). In the .<strong>NET</strong> Framework, <strong>XML</strong> readersprovide an effective way to deal with hierarchical, <strong>XML</strong>-shaped data.Because <strong>XML</strong> is just a metalanguage used to describe <strong>in</strong><strong>for</strong>mation,and not a data repository itself, the key difference between standard<strong>XML</strong> readers and custom <strong>XML</strong> readers is <strong>in</strong> the location and themodality of <strong>in</strong>tervention of the code that exposes data as <strong>XML</strong>. Suchcode is not part of the basic .<strong>NET</strong> <strong>XML</strong> reader classes butconstitutes the core of custom <strong>XML</strong> readers.Mapp<strong>in</strong>g Data Structures to <strong>XML</strong> NodesFor a long time, INI files have been a fundamental part of <strong>Microsoft</strong> W<strong>in</strong>dowsapplications. Although with the advent of <strong>Microsoft</strong> W<strong>in</strong>32 they were officially declaredobsolete, a lot of applications have not yet stopped us<strong>in</strong>g them. Understand<strong>in</strong>g thereasons <strong>for</strong> this persistence is not of much importance here, but when they weredesign<strong>in</strong>g the .<strong>NET</strong> Framework, the <strong>Microsoft</strong> architects decided not to <strong>in</strong>sert anymanaged classes to handle INI files. Although overall I agree with their decision, keep<strong>in</strong> m<strong>in</strong>d that if you need to access INI files from with<strong>in</strong> a .<strong>NET</strong> Framework application,you'll f<strong>in</strong>d at your disposal only workarounds, not a direct solution.You could, <strong>for</strong> <strong>in</strong>stance, read and write the content of an INI file us<strong>in</strong>g file and I/Oclasses, or you might resort to mak<strong>in</strong>g calls to the underly<strong>in</strong>g W<strong>in</strong>32 unmanagedplat<strong>for</strong>m. Recently, however, I came across a rather illum<strong>in</strong>at<strong>in</strong>g MSDN article <strong>in</strong> whichan even better approach is discussed. (See the section "Further Read<strong>in</strong>g," on page 74,<strong>for</strong> details and the URL.) The idea is this: Why not wrap the contents of INI files <strong>in</strong>to an<strong>XML</strong> reader? INI files are not well-<strong>for</strong>med <strong>XML</strong> files, but a custom reader could easilymap the contents of an INI file's sections and entries to <strong>XML</strong> nodes and attributes.45

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

Saved successfully!

Ooh no, something went wrong!