Updated 2010-06-22: Added link to part 5.
There are two remaining approaches to providing structure that I have not yet covered: collections of elements and section groups. Element collections will be a topic for several future posts as there are a number of different approaches to creating element collections, and two of them make little sense without first covering configuration inheritance (see part 5).
Section groups are much simpler, and mostly a matter of declaring them.
Creating Section Groups
When custom sections are declared using the
element of a configuration file, one can declare both sections (with the
<sections> element, using attributes to define the associated
custom type and its name) and section groups.
To define a section group use the—unsurprisingly—
element. This needs a name and a type. Often the type will be
System.Configuration.ConfigurationSectionGroup which adds no behaviour
(but one can customise this, more below) and just allows programmatic access to
its child sections and section groups (there is no specified limit to
the depth of structure you can define—but don’t go overboard with
deep structures which will become increasingly hard to understand and maintain).
Which defines a
userSettings element to act as a container for (presumably)
multiple sections describing many aspects of user behaviour.
Note, replace the strong name for
Version=18.104.22.168, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”
for .NET versions 2, 3 and 3.5.
Reading Section Groups and Contained Sections
In previous parts of this series the application code used
Configuration.GetSection(‹name›) to get
to its custom section (and the instance of
Configuration was obtained
This works for configuration sections that are immediate children of the root
</p> To get a section defined with a group simple get the group, and open the section from within that group. For instance, based on the above sample configuration:
GetSectionGroup returns a
GetSectionGroup methods as
Configuration Section and Group Collection Properties
You might notice that the
ConfigurationSectionGroup also have properties
can be indexed by name. These give another approach to getting
a section or section group:
are completely equivalent.
Custom Section Group Types
It is also possible to create your own custom configuration section
group by deriving from
a number of .NET Framework types do this. E.g.
defined in the
System.Xml assembly, which adds properties
to type safely access contained sections (i.e. the properties
contain the access into the
and cast the results to the correct type).