Working with custom sections in app|web.config

Requirement may arise to define a custom section in app.config or web.config configuration file(s), to load a related but dynamic data for configuration purpose.

Following is an example of how this can be achieved; element contains a sub element where sections can be declared with

element. The important thing about custom sections is the ability to define multiple name-value pairs and load’em at once in the .NET code. Here is the sample configuration file with aforementioned (and henceforth defined custom) elements.

<configuration> <configSections> <section name="Partners" type="System.Configuration.NameValueFileSectionHandler, System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> </configSections> <Partners> <!-- custom section--> <add key="000001" value="eBay Inc."/> <add key="000002" value="Amars Inc."/> </Partners> <appSettings> <add key="dbStr" value="Data Source=localhost;uid=user;pwd=password;database=master"/> </appSettings> </configuration>