I want to have the following structure in my config:
<MySection>
<add key="1" value="one" />
<add key="2" value="two" />
<add key="3" value="three" />
</MySection>
I have a limitation such that MySection can't use the AppSettingsSection as it has to inherit from a different parent custom section. And I need to resolve this section to a NameValueCollection such that when I call something like:
ConfigurationManager.GetConfig("MySection")
it should return a NameValueCollection. How to go about doing this? I found some information on NameValueConfigurationCollection but that's not what I'm looking for.
This worked --
Code:
Configuration:
My major concern was that my section needed to inherit from a custom section and I wanted to return a NameValueCollection when the ConfigurationManager.GetSection("MyAppSettings") is called.
I changed the type property to AppSettingsSection, even though it's nowhere in the picture and it worked. Now I need to figure out how that worked but for now the good thing is that I have a working sample :)
Update: Unfortunately, this wasn't the expected way of achieving what was intended because now the custom section is not coming into the picture at all so unfortunately this isn't the best way to do it.
Of course, if you just wanted to rename your appsettings section this would work like a charm.
you should create a class which derives from
ConfigurationSection
see a full example here: How to: Create Custom Configuration Sections Using ConfigurationSection