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.

NoteUser applications don't need to declare the sectionbecause the section is already declared <strong>in</strong> the system'smach<strong>in</strong>e.config file, as we saw <strong>in</strong> the section "Mach<strong>in</strong>eConfiguration Files," on page 626. You don't need to repeat the declaration unless you want to modify some of theattributes, <strong>in</strong>clud<strong>in</strong>g the name/value <strong>for</strong>mat of the sett<strong>in</strong>gs.The ConfigurationSett<strong>in</strong>gs ClassTo programmatically read application sett<strong>in</strong>gs, you use the ConfigurationSett<strong>in</strong>gs class.ConfigurationSett<strong>in</strong>gs is a small, sealed class that simply provides one static method(GetConfig) and one static property (AppSett<strong>in</strong>gs).The AppSett<strong>in</strong>gs property is a read-only NameValueCollection object designed to getthe <strong>in</strong><strong>for</strong>mation stored <strong>in</strong> the section. If no sett<strong>in</strong>g is specified, or if no section exists, an empty collection is returned.NoteTo have a read-only NameValueCollection object, you need to usea class that derives from NameValueCollection and sets theprotected member IsReadonly to true. This is exactly what happensunder the hood of the AppSett<strong>in</strong>gs property. The helper collectionclass that the AppSett<strong>in</strong>gs property returns is an undocumentedclass named ReadOnlyNameValueCollection.The GetConfig method returns the configuration sett<strong>in</strong>gs <strong>for</strong> the specified section, asshown here:public static object GetConfig(str<strong>in</strong>g sectionName);Although the method signature <strong>in</strong>dicates an object return type, the actual return valueyou get from a call to GetConfig is a class derived from NameValueCollection. Inparticular, the class is ReadOnlyNameValueCollection if the section is .NoteIn general, the object returned by GetConfig is determ<strong>in</strong>ed by thehandler class specified <strong>for</strong> the section. If the handler isNameValueSectionHandler or a related class, you get sett<strong>in</strong>gsstored <strong>in</strong> a name/value collection. As we'll see chapter <strong>in</strong> the section"Types of Section Handlers," on page 640, other options exist thatcould result <strong>in</strong> a different way of pack<strong>in</strong>g sett<strong>in</strong>gs <strong>for</strong> applications.The AppSett<strong>in</strong>gs property acts as a wrapper <strong>for</strong> the GetConfig method. The actualimplementation of the property consists of a call to GetConfig <strong>in</strong> which the section namedefaults to . The follow<strong>in</strong>g pseudocode demonstrates:public static NameValueCollection AppSett<strong>in</strong>gs{get {return GetConfig("appSett<strong>in</strong>gs");}}The real code is a bit more sophisticated than this, however. After GetConfig returns,the get accessor verifies that the returned value is not null. GetConfig returns null if thespecified section is empty or does not exist. If the returned object is null, the getaccessor of the AppSett<strong>in</strong>gs property creates an empty collection and returns that tothe caller. The pseudocode is shown here:public static NameValueCollection AppSett<strong>in</strong>gs{510

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

Saved successfully!

Ooh no, something went wrong!