4

In my app.config file I have a custom section under configuration with multiple entries that share the same key.

<setion1>
    <add key="key1" value="value1"/>
    <add key="key2" value="value2"/>
    <add key="key1" value="value3"/>
</section1>

I'm using the following code to get a NameValueCollection object from reading the entries.

var list = (NameValueCollection)ConfigurationManager.GetSection("section1");

I expected this code to return each entry under the section however it seems to only bring back unique values with respect to the key. How can I collect all the children of <section1> irrespective of the keys?

Siavash
  • 2,813
  • 4
  • 29
  • 42
  • The key is supposed to be unique, so it's by design that only the last added value is returned. The question is, why do you have duplicate keys? And what do you plan to do with them? – marsze Oct 23 '18 at 11:40
  • I have to store mail recipients in the app.config. Each section has it's own list of MailTo and CC entries and the section name dictates which group to send the mail out to. So I need to be able to retrieve all the entries within the section and either MailTo or CC the recipient based on key. – mitchsnitchel Oct 23 '18 at 11:44
  • @marsze: where is it documented that the key of a NameValueCollection is supposed to be unique? I thought that the behaviour was like described [here](https://learn.microsoft.com/en-us/dotnet/api/system.collections.specialized.namevaluecollection.add?redirectedfrom=MSDN&view=netframework-4.7.2#System_Collections_Specialized_NameValueCollection_Add_System_String_System_String_). So that the values that belong to a duplicate key are concatenated with comma. – Tim Schmelter Oct 23 '18 at 11:44
  • It's not so much the implementation of `NameValueCollection` but the configuration section handler. – marsze Oct 23 '18 at 11:46
  • @marsze: you're right, at least the source shows this behaviour: https://referencesource.microsoft.com/#System/sys/system/configuration/NameValueSectionHandler.cs,2bdf51ff956cfcde,references But still haven't found documentation – Tim Schmelter Oct 23 '18 at 11:49
  • @TimSchmelter That's about the common behavior for collections in config sections. Either way, `NameValueCollection` concatenates values for duplicate keys, which isn't overly useful. – marsze Oct 23 '18 at 12:10

2 Answers2

1

You should not use NameValueCollection. It has bad performance and concatenates values for duplicate keys.

You could use KeyValuePair´s and create your own handler for that:

using System;
using System.Configuration;
using System.Collections.Generic;
using System.Xml;
using KeyValue = System.Collections.Generic.KeyValuePair<string, string>;

namespace YourNamespace
{
    public sealed class KeyValueHandler : IConfigurationSectionHandler
    {
        public object Create(object parent, object configContext, XmlNode section)
        {
            var result = new List<KeyValue>();
            foreach (XmlNode child in section.ChildNodes)
            {
                var key = child.Attributes["key"].Value;
                var value = child.Attributes["value"].Value;
                result.Add(new KeyValue(key, value));
            }
            return result;
        }
    }
}

Configuration:

<configSections>
  <section name="section1" type="YourNamespace.KeyValueHandler, YourAssembly" />
</configSections>
<setion1>
    <add key="key1" value="value1"/>
    <add key="key2" value="value2"/>
    <add key="key1" value="value3"/>
</section1>

Usage:

var list = (IList<KeyValue>)ConfigurationManager.GetSection("section1");
marsze
  • 15,079
  • 5
  • 45
  • 61
1

keys have to be by definition unqiue.

"I have to store mail recipients in the app.config. Each section has it's own list of MailTo and CC entries and the section name dictates which group to send the mail out to."

Then you do not have a bunch of key/mail pairs.

You have a bunch of key/mail[] pairs.

For each key, you have a collection of values. So you use a collection of values. To wich the answer would be this: https://stackoverflow.com/a/1779453/3346583

Of course in that case, scaleability might be a problem. But if you need scaleabiltiy, you propably should solve that as a 1:N relationship in a database/XML File/other data structure anyway. Rather then app.onfig entries.

Christopher
  • 9,634
  • 2
  • 17
  • 31