I've searched the site and haven't found exactly what I'm looking for. Close, but no cigar.
Basically I want to have a config section like this:
<configSections>
<section name="PhoneNotificationsSection" type="Alerts.PhoneAlertConfigSection,Alerts,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null"/>
</configSections>
<PhoneNotificationsSection>
<phones>
<add phone="MyMobile" value="1234567890@vtext.com" />
<add phone="OtherMobile" value="1234567890@txt.att.com" />
</phones>
</PhoneNotificationsSection>
Then I'd like to, in my appSettings consuming code, be able to write something like this (pseudo code):
foreach (phone p in phones)
{
//'phone' attribute is just helpful/descriptive
DoSomething(p.value);
}
I've done enough research to know I probably need a few of my own classes that implement and/or inherit from certain Configuration classes to make the above code possible. I just haven't found anything that clearly demonstrates this scenario and how to code for it - and when I try to learn the whole .NET configuration world my brain starts to hurt. Anyone have some code like what I'm looking for that they can share?
I've written something similar once, as an example for a C# course. In my opinion it mainly demonstrates how awful the .NET configuration subsystem is, although the code does work. I've not adapted it to your settings, as it's fairly easy to introduce a mistake and so far the SO editor does not validate posted code samples ;)
First, the configuration section declaration:
To match the above snippet we first need the configuration section:
We are using a custom element collection, so let's declare that too:
And finally we need to declare the custom element used in the element collection:
Now, having all this in place we're ready to access the configuration file. I'm using a Configurator helper class to make this slightly less cumbersome:
Hope this helps.