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.

get{ReadOnlyNameValueCollection o = GetConfig("appSett<strong>in</strong>gs");if (o == null){o = new ReadOnlyNameValueCollection();o.IsReadOnly = true;}}return o;}Internally, the GetConfig method first determ<strong>in</strong>es the name and location of theconfiguration file to access and then proceeds by creat<strong>in</strong>g a specialized <strong>XML</strong> textreader to operate on the <strong>XML</strong> document. Each <strong>XML</strong> node read is parsed and thecontents stored as name/value pairs <strong>in</strong> a ReadOnlyNameValueCollection object. Toparse the contents of each <strong>XML</strong> node found, the method uses an <strong>in</strong>stance of thesection handler class specified <strong>in</strong> the section declaration with<strong>in</strong> the block. To read the section, GetConfig resorts to theNameValueSectionHandler handler. This handler parses all the nodes below and adds entries to the collection. We'll look at section handler objects<strong>in</strong> more detail <strong>in</strong> the section "Customiz<strong>in</strong>g the <strong>XML</strong> Schema <strong>for</strong> Your Data," on page646.The Section HandlerIn our sample mach<strong>in</strong>e.config file, the section is read through an<strong>in</strong>stance of the NameValueFileSectionHandler class. What's the difference betweenthis class and the NameValueSectionHandler class?The MSDN documentation doesn't provide further <strong>in</strong><strong>for</strong>mation about theNameValueFileSectionHandler class; it notes only that the class is <strong>in</strong>tended to be usedonly by the .<strong>NET</strong> Framework. But the NameValueFileSectionHandler class is actually awrapper <strong>for</strong> NameValueSectionHandler class, which provides an extra, althoughundocumented, feature. In particular, the NameValueFileSectionHandler sectionhandler allows the application sett<strong>in</strong>gs to be stored <strong>in</strong> a separated file <strong>in</strong> accordancewith the follow<strong>in</strong>g syntax:The file po<strong>in</strong>ted to by the file attribute is read as if it is an section <strong>in</strong> theconfiguration file. Note that the root element of the myfile.config file must match thesection that refers to it. So if the file attribute belongs to the section, theroot element of the file be<strong>in</strong>g po<strong>in</strong>ted to must be named .The NameValueFileSectionHandler object processes the contents of the embedded fileus<strong>in</strong>g the NameValueSectionHandler class. If no file is embedded <strong>in</strong> the section but the default documented schema is used, the two section handlers arefunctionally equivalent.Although undocumented, the follow<strong>in</strong>g code represents a perfectly valid schema <strong>for</strong> theapplication's configuration file. The sample application AppSett<strong>in</strong>gs, available <strong>in</strong> thisbook's sample files, demonstrates how to take advantage of this syntax.511

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

Saved successfully!

Ooh no, something went wrong!