[RESOLVED] Retrieving elements from ConfigurationSections in App.Config
I'm trying to to store grouped configuration settings in my app.config, but I'm having difficulty in drilling down to the lowest level. My config file looks like this:
XML Code:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="deploymentEnvironments">
<section name="Dev" type="System.Configuration.NameValueSectionHandler" />
<section name="SIT" type="System.Configuration.NameValueSectionHandler" />
<section name="UAT" type="System.Configuration.NameValueSectionHandler" />
<section name="Live" type="System.Configuration.NameValueSectionHandler" />
</sectionGroup>
</configSections>
<deploymentEnvironments>
<Dev>
<Database>DevDatabaseName</Database>
<Server>DevDatabaseServer</Server>
<LogLocation>DevLogLocation</LogLocation>
</Dev>
<SIT>
<Database>SITDatabaseName</Database>
<Server>SITDatabaseServer</Server>
<LogLocation>SITLogLocation</LogLocation>
</SIT>
<UAT>
<Database>UATDatabaseName</Database>
<Server>UATDatabaseServer</Server>
<LogLocation>UATLogLocation</LogLocation>
</UAT>
<Live>
<Database>LiveDatabaseName</Database>
<Server>LiveDatabaseServer</Server>
<LogLocation>LiveLogLocation</LogLocation>
</Live>
</deploymentEnvironments>
<appSettings>
<add key="Environment" value="Dev"/>
</appSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
In VB, I read it like this:
VB.Net Code:
Dim config As Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
Dim sectionGroups As ConfigurationSectionGroupCollection = config.SectionGroups
Dim sectionGroup As ConfigurationSectionGroup = sectionGroups("deploymentEnvironments")
'# This correctly gets the "Dev" group
Dim configurationSection As ConfigurationSection = sectionGroup.Sections(ConfigurationManager.AppSettings("Environment"))
Dim section As NameValueCollection
'# configurationSection.SectionInformation.Name returns the name "Dev". This line gives "section" a value of Nothing.
section = CType(ConfigurationManager.GetSection(configurationSection.SectionInformation.Name), NameValueCollection)
'# configurationSection.SectionInformation.SectionName returns the name "deploymentEnvironments/Dev". This throws an
'# error of "Unrecognized element."""
section = CType(ConfigurationManager.GetSection(configurationSection.SectionInformation.SectionName), NameValueCollection)
Any idea how I can get to the elements (Database, Server etc) within the Dev section?
Re: Retrieving elements from ConfigurationSections in App.Config
You can navigate the elements in the app.config which is an XML file using XMLReader class or LINQ to XML approach.
- kgc
Re: Retrieving elements from ConfigurationSections in App.Config
Re: Retrieving elements from ConfigurationSections in App.Config
Thank you both for the suggestions. The second one looked perfect and simple, but I now need to find where the NameValueSection object comes from, as it's not in System.Configuration (although there is a NameValueSectionHandler).
Re: Retrieving elements from ConfigurationSections in App.Config
Turns out I'd set my config up incorrectly. Instead of:
XML Code:
<UAT>
<Database>UATDatabaseName</Database>
<Server>UATDatabaseServer</Server>
<LogLocation>UATLogLocation</LogLocation>
</UAT>
... I should have had:
XML Code:
<UAT>
<add key="Database" value="UATDatabaseName"></add>
<add key="Server" value="UATDatabaseServer"></add>
<add key="LogLocation" value="UATLogLocation"></add>
</UAT>
This then works with the code:
VB.Net Code:
Dim config As System.Configuration.Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
Dim sectionGroups As ConfigurationSectionGroupCollection = config.SectionGroups
Dim sectionGroup As ConfigurationSectionGroup = sectionGroups("deploymentEnvironments")
Dim configurationSection As ConfigurationSection = sectionGroup.Sections("UAT")
Dim section As NameValueCollection = CType(ConfigurationManager.GetSection(configurationSection.SectionInformation.SectionName), NameValueCollection)
Dim server As String = section("Server")
Console.WriteLine(server)
Thanks for the help. :)
Re: [RESOLVED] Retrieving elements from ConfigurationSections in App.Config
Will try this out. nice! :thumb:
Re: [RESOLVED] Retrieving elements from ConfigurationSections in App.Config
Another solution is to use System.Configuration.AppSettingsSection type instead of NameValueSectionHandler for recent .NET
versions. How to get the values of a ConfigurationSection of type NameValueSectionHandler
- kgc
Re: [RESOLVED] Retrieving elements from ConfigurationSections in App.Config
Quote:
Originally Posted by
KGComputers
Another solution is to use System.Configuration.AppSettingsSection type instead
I think the point here is that there are multiple equivalent sections and you can choose one of those first and then treat them all the same after that.