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?




Reply With Quote