Ho avuto un problema simile e l'ho fatto con questo codice. Spero che questo aiuti nel tuo problema.
In questo caso Elenco (simile alla mia URLSection) avrà una sezione di configurazione completa in web.config che è possibile ottenere tutti i valori da questa sezione quindi.
<configSections>
<section name="URLSection" type="A.WebConfigSection,A,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null"/>
</configSections>
<appSettings></appSettings>
<URLSection>
<urlCollection>
<add url="1" value="a"/>
<add url="2" value="b"/>
</urlCollection>
</URLSection>
Ho creato tre classi per questo: ConfigElement, ConfigElementCollection, WebConfigSection.
ConfigElement
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
namespace A
{
public class ConfigElement:System.Configuration.ConfigurationElement
{
[ConfigurationProperty("url",IsRequired=true) ]
public string url
{
get
{
return this["url"] as string;
}
}
[ConfigurationProperty("value", IsRequired = true)]
public string value
{
get
{
return this["value"] as string;
}
}
}
}
ConfigElementCollection
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
namespace A
{
public class ConfigElementCollection:ConfigurationElementCollection
{
public ConfigElement this[int index]
{
get
{
return base.BaseGet(index) as ConfigElement;
}
}
protected override ConfigurationElement CreateNewElement()
{
return new ConfigElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((ConfigElement)(element)).url;
}
}
}
WebConfigSection
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
namespace A
{
public class WebConfigSection:ConfigurationSection
{
public WebConfigSection()
{
}
[ConfigurationProperty("urlCollection")]
public ConfigElementCollection allValues
{
get
{
return this["urlCollection"] as ConfigElementCollection;
}
}
public static WebConfigSection GetConfigSection()
{
return ConfigurationSettings.GetConfig("URLSection") as WebConfigSection;
}
}
}
fonte
2009-11-18 11:56:46
soluzione più semplice è quella di utilizzare [System.Collections.Specialized.StringCollection] (http://msdn.microsoft.com/en-us/library /system.collections.specialized.stringcollection.aspx): Risposta per la domanda: [Archivia serie di stringhe in appSettings?] (http://stackoverflow.com/q/10419116/155207) – Serkan