2

I am trying to implement a simple construct where I first clear the NameValueCollection and the write new values to it. However at the moment I can't because it is read only. Is there a way to make it not read only or a walk around? My Code:

NameValueCollection DataDictionary = ConfigurationManager.GetSection("dataDictionary") as NameValueCollection;
            DataDictionary.Clear();
            for (int i = 0; i < tableLayoutPanel2.RowCount; i++)
            {
                var T = tableLayoutPanel2.GetControlFromPosition(1, i);
                var T2 = tableLayoutPanel2.GetControlFromPosition(3, i);
                string key = T.Text;
                string value = T2.Text;
                DataDictionary.Add(key, value);
            }
Ben
  • 2,518
  • 4
  • 18
  • 31

1 Answers1

3

Copy it in a new NameValueCollection:

var writableCollection = new NameValueCollection(DataDictionary);
writableCollection.Add(...);
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • Yes this would work but I'm trying to rewrite back to the XML file so how could I do this with the new `writableCollection`? – Ben Dec 11 '14 at 16:55
  • [You can't](http://stackoverflow.com/questions/12306130/how-do-i-save-custom-configuration-sections-during-runtime). If you really must you can try using Xml library functions, but configuration sections apart from appSettings are designed to be read-only. – CodeCaster Dec 11 '14 at 16:58