Custom Elements

The appSettings element is actually a special case of the general mechanism for adding custom configuration elements, called the configuration section. A configuration section is simply an element in a configuration file. The ConfigSettings class knows how to read an arbitrary configuration section because you add a configSection element to the configuration file to tell it how. The appSettings configuration section is defined in the machine configuration file, as shown below:

<section name="appSettings" type="System.Configuration.NameValueFileSectionHandler, 
System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />

I’ll explain more about the section element in a moment. You can see, though, that the section element shows us that the appSettings section is handled by the System.Configuration.NameValueFileSectionHandler type. This type’s Create( ) method reads the XML that is passed to it as an XmlNode, and creates the System.Collections.Specialized.NameValueCollection that is eventually returned by the ConfigSettings.AppSettings property.

The steps to create your own custom configuration sections are as follows:

  1. Select a configuration section handler to handle the configuration section. If none of the built-in configuration section handlers are appropriate, write your own class that implements IConfigurationSectionHandler.

  2. Add a section element to the configSections element of the machine or application configuration file, which links ...

Get .NET & XML now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.