6

I want to create a custom configuration section to handle email notifications. The configuration needs to be in the following format:

<configSections>
    <sectionGroup name="notifications">
        <section name="notification" type="NotificationConfiguration" allowLocation="true" allowDefinition="Everywhere" />
    </sectionGroup>
</configSections>
...
<notifications>
    <notification name="..." enabled="..." delayInMinutes="...">
        <recipients>
            <add email="..." />
            <add email="..." />
            <add email="..." />
        </recipients>
    </notification>
    <notification name="..." enabled="..." delayInMinutes="...">
        <recipients>
            <add email="..." />
            <add email="..." />
            <add email="..." />
        </recipients>
    </notification>
</notifications>
...

I can get this to work fine using NotificationConfiguration config = (NotificationConfiguration) ConfigurationManager.GetSection("notifications\notification"), but this only caters for one <notification> element. How do I accomplish multiple elements to accommodate more than one notification?

The class that handles this is quite lengthy, so I won't paste it here, but it can be downloaded from here:

http://files.getdropbox.com/u/288235/NotificationConfiguration.cs

Thanks.

1
  • You can create a <NotificationsGroup /> outer element, and then put all your notification elements inside that group. This way, you'll be able to achieve what you want to achieve. Commented Jul 21, 2009 at 9:23

1 Answer 1

1

You can use the ConfigurationElementCollection Class.

A reference of how to use it can be found on CodeProject.

EDIT: You can create a <NotificationsGroup /> outer element, and then put all your notification elements inside that group. This way, you'll be able to achieve what you want to achieve.

EDIT 2:

<configSections>
    <sectionGroup name="NotificationsGroup">
        <section name="NotificationsGroup" type="NotificationGroupConfiguration" allowLocation="true" allowDefinition="Everywhere" />
    </sectionGroup>
</configSections>

<NotificationsGroup>
    <Notifications>
    </Notifications>
    ... Multiple notifications go here, instead of one.
    <Notifications>
    </Notifications>
</NotificationsGroup>

This means that NotificationsGroup will contain the element collection of Notifications.

Sign up to request clarification or add additional context in comments.

3 Comments

Hi Kirtan It is my understanding that ConfigurationElementCollection class is used for a collection of elements, not sections. I use that class for the <recipients> tag, but the <notification> tag is defined as a configuration section at the top of the web.config file, so how do I accomplish a collection of sections, as opposed to a collection of elements within a section?
At the top of the web.config, I define my custom configuration as follows: <configSections> <sectionGroup name="notifications"> <section name="notification" type="NotificationConfiguration" allowLocation="true" allowDefinition="Everywhere" /> </sectionGroup> </configSections> Do you mean a group of notifications, as in my <notifications> tag?
Thanks for your help Kirtran. The tool below eventually helped a lot too! codeplex.com/csd

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.