Results 1 to 8 of 8

Thread: [RESOLVED] Retrieving elements from ConfigurationSections in App.Config

  1. #1

    Thread Starter
    Fanatic Member InvisibleDuncan's Avatar
    Join Date
    May 2001
    Location
    Eating jam.
    Posts
    819

    Resolved [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:
    1. <?xml version="1.0" encoding="utf-8" ?>
    2. <configuration>
    3.     <configSections>
    4.         <sectionGroup name="deploymentEnvironments">
    5.             <section name="Dev" type="System.Configuration.NameValueSectionHandler" />
    6.             <section name="SIT" type="System.Configuration.NameValueSectionHandler" />
    7.             <section name="UAT" type="System.Configuration.NameValueSectionHandler" />
    8.             <section name="Live" type="System.Configuration.NameValueSectionHandler" />
    9.         </sectionGroup>
    10.     </configSections>
    11.     <deploymentEnvironments>
    12.         <Dev>
    13.             <Database>DevDatabaseName</Database>
    14.             <Server>DevDatabaseServer</Server>
    15.             <LogLocation>DevLogLocation</LogLocation>
    16.         </Dev>
    17.         <SIT>
    18.             <Database>SITDatabaseName</Database>
    19.             <Server>SITDatabaseServer</Server>
    20.             <LogLocation>SITLogLocation</LogLocation>
    21.         </SIT>
    22.         <UAT>
    23.             <Database>UATDatabaseName</Database>
    24.             <Server>UATDatabaseServer</Server>
    25.             <LogLocation>UATLogLocation</LogLocation>
    26.         </UAT>
    27.         <Live>
    28.             <Database>LiveDatabaseName</Database>
    29.             <Server>LiveDatabaseServer</Server>
    30.             <LogLocation>LiveLogLocation</LogLocation>
    31.         </Live>
    32.     </deploymentEnvironments>
    33.     <appSettings>
    34.         <add key="Environment" value="Dev"/>
    35.     </appSettings>
    36.     <startup>
    37.         <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    38.     </startup>
    39. </configuration>

    In VB, I read it like this:

    VB.Net Code:
    1. Dim config As Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
    2.         Dim sectionGroups As ConfigurationSectionGroupCollection = config.SectionGroups
    3.         Dim sectionGroup As ConfigurationSectionGroup = sectionGroups("deploymentEnvironments")
    4.  
    5.         '# This correctly gets the "Dev" group
    6.         Dim configurationSection As ConfigurationSection = sectionGroup.Sections(ConfigurationManager.AppSettings("Environment"))
    7.  
    8.         Dim section As NameValueCollection
    9.  
    10.         '# configurationSection.SectionInformation.Name returns the name "Dev". This line gives "section" a value of Nothing.
    11.         section = CType(ConfigurationManager.GetSection(configurationSection.SectionInformation.Name), NameValueCollection)
    12.  
    13.         '# configurationSection.SectionInformation.SectionName returns the name "deploymentEnvironments/Dev". This throws an
    14.         '# error of "Unrecognized element."""
    15.         section = CType(ConfigurationManager.GetSection(configurationSection.SectionInformation.SectionName), NameValueCollection)

    Any idea how I can get to the elements (Database, Server etc) within the Dev section?
    Indecisiveness is the key to flexibility.

    www.mangojacks.com

  2. #2
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,020

    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
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Retrieving elements from ConfigurationSections in App.Config

    The code is C# but it addresses your task:

    http://scottdorman.github.io/2008/03...uration-files/

  4. #4

    Thread Starter
    Fanatic Member InvisibleDuncan's Avatar
    Join Date
    May 2001
    Location
    Eating jam.
    Posts
    819

    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).
    Indecisiveness is the key to flexibility.

    www.mangojacks.com

  5. #5

    Thread Starter
    Fanatic Member InvisibleDuncan's Avatar
    Join Date
    May 2001
    Location
    Eating jam.
    Posts
    819

    Re: Retrieving elements from ConfigurationSections in App.Config

    Turns out I'd set my config up incorrectly. Instead of:

    XML Code:
    1. <UAT>
    2.    <Database>UATDatabaseName</Database>
    3.    <Server>UATDatabaseServer</Server>
    4.    <LogLocation>UATLogLocation</LogLocation>
    5. </UAT>

    ... I should have had:

    XML Code:
    1. <UAT>
    2.     <add key="Database" value="UATDatabaseName"></add>
    3.     <add key="Server" value="UATDatabaseServer"></add>
    4.     <add key="LogLocation" value="UATLogLocation"></add>
    5. </UAT>

    This then works with the code:

    VB.Net Code:
    1. Dim config As System.Configuration.Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
    2.         Dim sectionGroups As ConfigurationSectionGroupCollection = config.SectionGroups
    3.         Dim sectionGroup As ConfigurationSectionGroup = sectionGroups("deploymentEnvironments")
    4.  
    5.         Dim configurationSection As ConfigurationSection = sectionGroup.Sections("UAT")
    6.  
    7.         Dim section As NameValueCollection = CType(ConfigurationManager.GetSection(configurationSection.SectionInformation.SectionName), NameValueCollection)
    8.         Dim server As String = section("Server")
    9.         Console.WriteLine(server)

    Thanks for the help.
    Indecisiveness is the key to flexibility.

    www.mangojacks.com

  6. #6
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,020

    Re: [RESOLVED] Retrieving elements from ConfigurationSections in App.Config

    The code is C# but it addresses your task:

    http://scottdorman.github.io/2008/03...uration-files/
    Will try this out. nice!
    Last edited by KGComputers; Oct 7th, 2017 at 02:15 AM.
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

  7. #7
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,020

    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
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: [RESOLVED] Retrieving elements from ConfigurationSections in App.Config

    Quote Originally Posted by KGComputers View Post
    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
  •  



Click Here to Expand Forum to Full Width