-
Oct 4th, 2017, 08:25 AM
#1
Thread Starter
Fanatic Member
[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?
-
Oct 4th, 2017, 10:15 AM
#2
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
-
Oct 6th, 2017, 07:11 AM
#3
Re: Retrieving elements from ConfigurationSections in App.Config
-
Oct 6th, 2017, 09:06 AM
#4
Thread Starter
Fanatic Member
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).
-
Oct 6th, 2017, 11:18 AM
#5
Thread Starter
Fanatic Member
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.
-
Oct 6th, 2017, 11:15 PM
#6
Re: [RESOLVED] Retrieving elements from ConfigurationSections in App.Config
Will try this out. nice!
Last edited by KGComputers; Oct 7th, 2017 at 02:15 AM.
-
Oct 7th, 2017, 02:17 AM
#7
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
-
Oct 7th, 2017, 03:32 AM
#8
Re: [RESOLVED] Retrieving elements from ConfigurationSections in App.Config
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|