0

I want to create a config list object from a my app.config file. This is what the config file looks like:

<configuration>

 <appSettings>
    <add key="SomeKey" value="some key's value"/>
    <add key="AnotherKey" value="another key's value"/>
 </appSettings>

 <ExtraSettings>
  <Url = "Some url here" Timeout="" Active="true" Source="some other value">
  <Url = "Some url here" Timeout="" Active="true" Source="some other value">
 </ExtraSettings>
</configuration>

App settings I can get using the regular .Net ConfigurationManager class. The problem is I am not able to create a list of my Settings class from the ExtraSettings section as above to map to my ExtraSettings class as below

public class ExtraSettings
{
   public string SomeKey { get; set; }
   public string AnotherKey{ get; set; }
   public List<Settings> SettingsList { get; set; }
}


public class Settings
{
    [ConfigurationProperty("Url")]
    public string Url { get; set; }
    [ConfigurationProperty("Timeout")]
    public string Timeout { get; set; }
   [ConfigurationProperty("Source")]
    public string Source { get; set; }
   [ConfigurationProperty("Active")]
    public string Active { get; set; }

}

I am stuck at this section in my code.

 var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            List<Settings> settingsList = config.GetSection("ExtraSettings")  ;

Am I in the right direction? What changes do I need to make?

user20358
  • 14,182
  • 36
  • 114
  • 186
  • I believe what you are asking is a duplicate in the above question. If this does not work, feel free to add more detail to your question. Additional examples [here](http://stackoverflow.com/questions/1316058/how-to-create-custom-config-section-in-app-config) and [here](http://stackoverflow.com/questions/758986/custom-app-config-config-section-handler) – SwDevMan81 Feb 12 '15 at 15:14
  • Thanks. That looks like it will work. I was however looking at a more simpler way to do it if its provided by the framework 4.5. Something like the equivalent of a config.GetSection("ExtraSettings").ToList – user20358 Feb 12 '15 at 16:06
  • tried it with the exact same code as in the answer. In the foreach loop I wasnt able to get the e.Name & e.Code. All I got was Equals, GetHashcode, GetType and ToString on the object e – user20358 Feb 13 '15 at 11:25
  • Yeah, looks like you need to change `foreach (var e in config.Instances)` to `foreach (MyConfigInstanceElement e in config.Instances)`. Let me know if that doesnt fix it. I've provided the full example [here] that includes the indexer as well (http://stackoverflow.com/a/28504466/95573) – SwDevMan81 Feb 13 '15 at 16:44
  • no that didn't work either. This line "ConfigurationManager.GetSection("registerCompanies") as MyConfigSection;" returns null. I tried using MyConfigSection as the type to return into and that still got null. I am pretty sure this should have worked as it seems to have worked with others. I am now planning on using a standard xml file instead. Reading a Config file should not be so much hassle. :) – user20358 Feb 16 '15 at 10:42
  • Do you see the configuration file in output? You need to make sure the app.config setting is set to `Content` – SwDevMan81 Feb 16 '15 at 15:11

0 Answers0