Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading custom configuration section's key values in C#

I need to read key values from custom sections in app/web.config.

I went through

Reading a key from the Web.Config using ConfigurationManager

and

How can I retrieve list of custom configuration sections in the .config file using C#?

However, they do not specify how to read a custom section when we need to explicitly specify the path to the configuration file (in my case, the configuration file is not in it's default location)

Example of my web.config file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <MyCustomTag> 
    <add key="key1" value="value1" />
    <add key="key2" value="value2" />
  </MyCustomTag>
<system.web>
  <compilation related data />
 </system.web> 
</configuration>

in which i need to read key value pairs inside MyCustomTag.

When i try (configFilePath is the path to my configuration file):-

var configFileMap = new ExeConfigurationFileMap { ExeConfigFilename = configFilePath };

var config =
          ConfigurationManager.OpenMappedExeConfiguration(
            configFileMap, ConfigurationUserLevel.None);

        ConfigurationSection section = config.GetSection(sectionName);

        return section[keyName].Value;

I get a error stating "Cannot access protected internal indexer 'this' here" at section[keyName]

like image 461
user720694 Avatar asked May 13 '13 14:05

user720694


2 Answers

Unfortunately, this is not as easy as it sounds. The way to solve the problem is to get file config file with ConfigurationManager and then work with the raw xml. So, I normally use the following method:

private NameValueCollection GetNameValueCollectionSection(string section, string filePath)
{
        string file = filePath;
        System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument();
        NameValueCollection nameValueColl = new NameValueCollection();

        System.Configuration.ExeConfigurationFileMap map = new ExeConfigurationFileMap();
        map.ExeConfigFilename = file;
        Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
        string xml = config.GetSection(section).SectionInformation.GetRawXml();
        xDoc.LoadXml(xml);

        System.Xml.XmlNode xList = xDoc.ChildNodes[0];
        foreach (System.Xml.XmlNode xNodo in xList)
        {
            nameValueColl.Add(xNodo.Attributes[0].Value, xNodo.Attributes[1].Value);

        }

        return nameValueColl;
 }

And the call of the method:

 var bla = GetNameValueCollectionSection("MyCustomTag", @".\XMLFile1.xml");


for (int i = 0; i < bla.Count; i++)
{
    Console.WriteLine(bla[i] + " = " + bla.Keys[i]);
}

The result:

enter image description here

like image 91
Kai Avatar answered Sep 27 '22 23:09

Kai


Formo makes it really easy, like:

dynamic config = new Configuration("customSection");
var appBuildDate = config.ApplicationBuildDate<DateTime>();

See Formo on Configuration Sections

like image 23
Jogai Avatar answered Sep 27 '22 22:09

Jogai