6

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]

8
  • 1
    Please, add your web.config (complete or partial). Commented May 13, 2013 at 14:35
  • There is nothing to do with the path of the custom settings in the web.config. you will be using <MyCustomTag configSource="customConfig.config" /> Commented May 13, 2013 at 14:38
  • @FrancescoDeLisi Question edited. Commented May 14, 2013 at 5:01
  • @saravanan what is customConfig in this case? Commented May 14, 2013 at 5:02
  • @user85030: it is the file that will contain all of your <add ... elements within <MyCustomTag> Commented May 14, 2013 at 5:26

2 Answers 2

8

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

Sign up to request clarification or add additional context in comments.

3 Comments

This answer does not mention why this approach is better than defining a custom section class (it is kind of more straightforward for simple case, but still).
@Kai Thanks. I've accepted your answer. @".\XMLFile1.xml" is the path to the configuration file, right?
@Kai It works! Also, i've replaced NameValueCollection with dictionaries for better performance.
1

Formo makes it really easy, like:

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

See Formo on Configuration Sections

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.