Solved

How to loop through <sectionGroup> elements in app.config file?

Posted on 2008-10-13
6
6,517 Views
Last Modified: 2012-05-05
The <configSections> in my app.config file consists of multiple sectionGroups. The program that reads my app.config needs to retrieve values from each section within each sectionGroup. Each section will consists of the same key names (with different values). The program does not know how many sectionGroups there will be and how many <section> elements each sectionGroup will consist of. That's why it needs to loop through all sectionGroups within <configSections> and all sections within <sectionGroup>. I searched on the internet and in different forums for an answer, but still can't figure out how to do that. I would appreciate your help. Thank you.

  <configSections>
    <sectionGroup name="Group1">
      <section name="1" type="System.Configuration.DictionarySectionHandler" />
      <section name="2" type="System.Configuration.DictionarySectionHandler" />
      <section name="3" type="System.Configuration.DictionarySectionHandler" />
    </sectionGroup>
    <sectionGroup name="Group2">
      <section name="1" type="System.Configuration.DictionarySectionHandler" />
      <section name="2" type="System.Configuration.DictionarySectionHandler" />
      <section name="3" type="System.Configuration.DictionarySectionHandler" />
    </sectionGroup>
 </configSections>
0
Comment
Question by:EwS
  • 4
6 Comments
 
LVL 28

Expert Comment

by:strickdd
Comment Utility
0
 
LVL 16

Expert Comment

by:Gyanendra Singh
Comment Utility
0
 

Author Comment

by:EwS
Comment Utility
I appreciate your quick response, but this is not what I'm looking for. For instance, the following line of code:
NameValueCollection servers = (NameValueCollection) ConfigurationManager.GetSection("mySectionGroup/mySection");
will retrieve the section for me and then I can loop through its key-value pairs, but I don't what to hard-code the sectionGroup name and the section name - I want to be able to retrieve them dynamically by looping through all the elements contained within <configSections> (just like I'm looping through the key-value pairs for each section). The second link (by BondinASP) also has the names hard-coded.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:EwS
Comment Utility
I found this code on http://www.codeproject.com/KB/cs/SystemConfiguration.aspx?display=Print that loops through all section groups and sections in app.config. The only problem that I'm having is group.IsDeclared is always false. Also, the group names and section names are not returned in the order in which they are declared in the config file, which is not a big deal as long as all of them are being returned, I was just wondering why that's the case.

// Get the application configuration file.
   System.Configuration.Configuration config =
     ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

   // Get the collection of the section groups.
   ConfigurationSectionGroupCollection sectionGroups = config.SectionGroups;

   // Show the configuration values
   ShowSectionGroupCollectionInfo(sectionGroups);

   static void ShowSectionGroupCollectionInfo(
          ConfigurationSectionGroupCollection sectionGroups)
   {
        ClientSettingsSection clientSection;
        SettingValueElement value;

        foreach(ConfigurationSectionGroup group in sectionGroups)
        // Loop over all groups
        {
            if(!group.IsDeclared)
            // Only the ones which are actually defined in app.config
              continue;

            Console.WriteLine("Group {0}", group.Name);

               // get all sections inside group
            foreach(ConfigurationSection section in group.Sections)
            {
              clientSection = section as ClientSettingsSection;
              Console.WriteLine("\tSection: {0}", section);

              if(clientSection == null)
                continue;


              foreach(SettingElement set in clientSection.Settings)
              {
                   value = set.Value as SettingValueElement;
                   // print out value of each section
                   Console.WriteLine("\t\t{0}: {1}",
                   set.Name,value.ValueXml.InnerText);
              }
        }
    }
0
 

Author Comment

by:EwS
Comment Utility
Does anyone know the answer to my question? I would expect to get some help from this site, but nobody provided me with the appropriate code - the one that I found myself is only partially working - nobody from all the experts knows the solution? I don't mean to sound rude, I'm just looking for help. Thanks!
0
 

Accepted Solution

by:
EwS earned 0 total points
Comment Utility
I found a solution to my problem. I changed the way I retrieve the names of section groups and sections from my app.config file. I'm using xml, which is less elegant, but more straightforward (I don't need to loop through section groups that are not declared, and section groups and sections are returned in the order they are defined.

Since I found a solution that works myself, can I, please, get my 500 points back?
Thank you.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now