Tôi đã xử lý vấn đề tương tự và tôi đã làm điều đó với mã này. Hy vọng điều này sẽ giúp trong vấn đề của bạn.
Trong trường hợp này Danh sách (tương tự như URLSection của tôi) sẽ có một phần cấu hình đầy đủ trong web.config mà bạn có thể nhận được tất cả các giá trị từ phần này sau đó.
<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>
Tôi đã tạo ba lớp cho điều này: 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;
}
}
}
Nguồn
2009-11-18 11:56:46
giải pháp đơn giản nhất là sử dụng [System.Collections.Specialized.StringCollection] (http://msdn.microsoft.com/en-us/library /system.collections.specialized.stringcollection.aspx): Đã trả lời cho câu hỏi: [Chuỗi chuỗi cửa hàng trong appSettings?] (http://stackoverflow.com/q/10419116/155207) – Serkan