For anyone else who found this answer like I did, I've refined the answer to use more standard parts of the ConfigurationManager mark-up to reduce the amount of boiler plate code required:
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
namespace TestSite
{
public class SiteConfiguration : ConfigurationSection
{
[ConfigurationProperty("listValues", DefaultValue = null, IsRequired = true)]
[ConfigurationCollection(typeof(ListValues),
AddItemName = "add",
ClearItemsName = "clear",
RemoveItemName = "remove")]
public ListValues ListValues
{
get { return (ListValues)this["listValues"]; }
set { this["listValues"] = value; }
}
}
/// <summary>
/// Boilder plate holder for the collection of values
/// </summary>
public class ListValues : ConfigurationElementCollection, IEnumerable<ConfigurationElement>
{
protected override ConfigurationElement CreateNewElement() { return new ListElement(); }
protected override object GetElementKey(ConfigurationElement element)
{
return ((ListElement)element).Value;
}
IEnumerator<ConfigurationElement> IEnumerable<ConfigurationElement>.GetEnumerator()
{
return this.OfType<ListElement>().GetEnumerator();
}
}
/// <summary>
/// Boilder plate holder for each value
/// </summary>
public class ListElement : ConfigurationElement
{
[ConfigurationProperty("value")]
public string Value
{
get { return (string)this["value"]; }
set { this["value"] = value; }
}
}
}
With the appropriate web.config:
<configSections>
<section name="siteConfiguration" type="TestSite.SiteConfiguration, TestSite"/>
</configSections>
<siteConfiguration>
<listValues>
<clear/>
<add value="one"/>
<add value="two"/>
<add value="three"/>
<add value="four"/>
<add value="five"/>
</listValues>
</siteConfiguration>
Which can then be used like so:
List<string> list = new List<string>();
ListValues values = ((SiteConfiguration)ConfigurationManager.GetSection("siteConfiguration")).ListValues;
foreach (ListElement elem in values)
{
list.Add(elem.Value);
}
And voila, all the values are now in a list. (Tested in .Net Framework 4.8)