Configuration types in Microsoft .NET

17Nov11

Here is a quick post to outline the different ways that .NET custom config files can be written. The MSDN documentation does a bad job generally at giving examples of what the actual XML markup looks like in their examples. They go through all of the different types involved without showing what the desired markup looks like.

Briefly, by default, custom configuration uses XML attributes, unlike the AppSettings section, which uses a special configuration case of a key/value dictionary. I think that this is confusing and is not well treated in MSDN.

If we want a custom appSettings-like config like the following:

<myAppSettings>
      <add key="NewKey0" value="Monday, March 30, 
           2009 1:36:33 PM" />
      <add key="NewKey1" value="Monday, March 30, 
           2009 1:36:40 PM" />
  </myAppSettings>

We need to use a DictionarySectionHandler instead of the usual ConfigurationSection.

Using a ConfigurationSection, our XML markup would look like this:

<myAppSettings
     NewKey0="Monday, March 30, 2009 1:36:33 PM"
     NewKey1="Monday, March 30, 2009 1:36:40 PM"
/>

It is possible to create elements as children with some additional coding using ConfigurationElement.

There is a long CodeProject article on this stuff that uses C++ here that I found useful and there is an extensive treatment on configuration here, but I’ve abridged things greatly here for my own reference. Hopefully this helps you too.

Advertisements


No Responses Yet to “Configuration types in Microsoft .NET”

  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: