Web.config - custom section with list of items
Your web.config sections:
<section name="departmentSettings" type="Namespace.ConfigurationSections.DepartmentSettings, AssemblyName" />
Your web.config custom section:
<departmentSettings> <departments> <department departmentId="department1" name="My Super Department 1" email="[email protected]" /> <department departmentId="department2" name="My Awesome Department 2" email="[email protected] " /> <department departmentId="department3" name="My Amazing Department 3" email="[email protected] " /> <department departmentId="department4" name="My Beautiful Department 4" email="[email protected] " /> </departments> </departmentSettings>
Your class:
public class DepartmentSettings : ConfigurationSection { private static DepartmentSettings _settings = ConfigurationManager.GetSection("departmentSettings") as DepartmentSettings; { get { return _settings; } } public DepartmentCollection Departments { get { return (DepartmentCollection)base["departments"]; } } } public class DepartmentCollection : ConfigurationElementCollection { protected override ConfigurationElement CreateNewElement() { return new DepartmentElement(); } { return ((DepartmentElement)element).DepartmentId; } } { [ConfigurationProperty("departmentId")] public string DepartmentId { get { return (string)this["departmentId"]; } set { this["departmentId"] = value; } } public string Name { get { return (string)this["name"]; } set { this["name"] = value; } } public string Email { get { return (string)this["email"]; } set { this["email"] = value; } } } public static DepartmentSettings Settings [ConfigurationProperty("departments")] [ConfigurationCollection(typeof(DepartmentElement), AddItemName = "department")] protected override object GetElementKey(ConfigurationElement element) public class DepartmentElement : ConfigurationElement [ConfigurationProperty("name")] [ConfigurationProperty("email")]











