I have an AppConfig file as shown below. I am trying to loop through the configsections and get the section name, based on the section name, it should select the appropriate appSettings. For example, when the first section is VehicleConfig, it should automatically select the appSettings of VehicleConfig. I need this automatic selection because i have multiple sections and I have to get the appSettings of different sections based on their section names.
<configuration>
<configSections>
<sectionGroup name="group1">
<section name="Vehiclefeature"type="System.Configuration.NameValueSectionHandler" />
<section name="LiveDownloader" type="System.Configuration.NameValueSectionHandler" />
</sectionGroup>
</configSections>
<VehicleFeature>
<add key="FileRoot" value="C:\FilesToUpload" />
<add key="Project" value="BigDataTest" />
<add key="Dataset" value="StoreServer" />
</VehicleFeature>
<LiveDownloader>
<add key="FileRoot" value="C:\FilesToUpload" />
<add key="Project" value="BigDataTest" />
<add key="Dataset" value="BQSMeasure" />
</LiveDownloader>
</configuration>
I have tried this code and when the second for-each loop is hit, it throws the error "Unrecognized element appSettings inside VehicleConfig". I tried removing the appSettings, but then it throws "Unrecognized element add". I wonder whether i can have these elements inside VehicleConfig.
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ConfigurationSectionGroupCollection sectionGroups = config.SectionGroups;
foreach (ConfigurationSectionGroup group in sectionGroups)
// Loop over all groups
{
Console.WriteLine(group);
if (group.Name == "FileCheckerConfigGroup")
{
foreach (ConfigurationSection configurationSection in group.Sections)
{
var section = ConfigurationManager.GetSection(configurationSection.SectionInformation.SectionName) as NameValueCollection;
}
}
}
Any help is appreciated !!