CDATA in Custom Configuration Sections

Nov. 28, 2010, 5:55 p.m.

Creating a custom configuration section with ConfigurationSection as described in this MSDN article is less straightforward when you want to use more than just attributes (e.g. text or CDATA elements):

<?xml version="1.0" encoding="utf-8" ?>
        <section name="stats" type="X.StatsSection, X"/>
        <sql><![CDATA[SELECT * FROM tbl WHERE x < y]]></sql>

If you do as told, you'll see this exception:

System.Configuration.ConfigurationErrorsException: The configuration section cannot contain a CDATA or text element.

Fortunately, creating a custom ConfigurationElement and overriding its DeserializeElement method does the trick:

class StatsSection : ConfigurationSection
    public SqlElement Sql { get { return this["sql"] as SqlElement; } }

class SqlElement : ConfigurationElement
    protected override void DeserializeElement(XmlReader reader, bool s)
        Value = reader.ReadElementContentAs(typeof(string), null) as string;

    public string Value { get; private set; }

Not rocket science but (hopefully now less) difficult to find online.