[ACCEPTED]-How do I store a dictionary object in my web.config file?-web-config

Accepted answer
Score: 129

Why reinvent the wheel? The AppSettings section is 7 designed for exactly the purpose of storing 6 dictionary-like data in your config file.

If 5 you don't want to put too much data in your 4 AppSettings section, you can group your 3 related values into their own section as 2 follows:

      type="System.Configuration.NameValueFileSectionHandler,System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />

     <add key="name1" value="value1" />
     <add key="name2" value="value2" />
     <add key="name3" value="value3" />
     <add key="name4" value="value4" />

You can access elements in this 1 collection using

using System.Collections.Specialized;
using System.Configuration;

public string GetName1()
    NameValueCollection section =
    return section["name1"];
Score: 27

Juliet's answer is on point, but FYI you 4 can also put additional configs in external 3 .config files, by setting up your web.config as follows:

<?xml version="1.0"?>
    <!-- blah blah the default stuff here -->

    <!-- here, add your custom section -->
    <section name="DocTabMap" type="System.Configuration.NameValueFileSectionHandler, System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />

  <!-- your custom section, but referenced in another file -->
  <DocTabMap file="CustomDocTabs.config" />

  <!-- etc, remainder of default web.config is here -->

Then, your 2 CustomDocTabs.config looks like this:

<?xml version="1.0"?>
  <add key="A" value="1" />
  <add key="B" value="2" />
  <add key="C" value="3" />
  <add key="D" value="4" />

Now you can access it in 1 code via:

NameValueCollection DocTabMap = ConfigurationManager.GetSection("DocTabMap") as NameValueCollection;
DocTabMap["A"] // == "B"
Score: 5

You would need to implement a custom section 7 (See Configuration Section Designer).

What you really want... is something 6 close to this:

  <add name="Something1" value="something else"/>
  <add name="Something2" value="something else"/>
  <add name="Something3" value="something else"/>

Where the XmlAttribute "name" is 5 a Key which it won't allow to have more 4 than one in the code behind. At the same 3 time, make sure that the Collection MyDictionary 2 is also a Dictionary.

You can do all of this 1 with this tool and fill the gap as needed.

Score: 3

In application settings we can use System.Collection.Specilized.StringCollection

  <setting name="ElementsList" serializeAs="Xml">
      <ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

Access 1 to list:

var element = Settings.Default.ElementsList[index]
Score: 0

I'm not sure how to store a Dictionary directly 6 but you could easily use an array of strings 5 to store a dictionary. For every key, value 4 pair you save out the key as the first string 3 and the value as the second. Then when 2 rebuilding the dictionary you can undo this 1 encoding.

static Dictionary<string,string> ArrayToDictionary(string[] data) {
  var map = new Dictionary<string,string>();
  for ( var i=  0; i < data.Length; i+=2 ) {
    map.Add(data[i], data[i+1]);
  return map;

More Related questions