Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I include a CDATA section in a ConfigurationElement?

I'm using the .NET Fx 3.5 and have written my own configuration classes which inherit from ConfigurationSection/ConfigurationElement. Currently I end up with something that looks like this in my configuration file:

<blah.mail>
    <templates>
        <add name="TemplateNbr1" subject="..." body="Hi!\r\nThis is a test.\r\n.">
            <from address="[email protected]" />
        </add>
    </templates>
</blah.mail>

I would like to be able to express the body as a child node of template (which is the add node in the example above) to end up with something that looks like:

<blah.mail>
    <templates>
        <add name="TemplateNbr1" subject="...">
            <from address="[email protected]" />
            <body><![CDATA[Hi!
This is a test.
]]></body>
        </add>
    </templates>
</blah.mail>
like image 843
cfeduke Avatar asked Jan 07 '09 21:01

cfeduke


1 Answers

In your custom configuration element class you need to override method OnDeserializeUnrecognizedElement.

Example:

public class PluginConfigurationElement : ConfigurationElement
{
    public NameValueCollection CustomProperies { get; set; }

    public PluginConfigurationElement()
    {
        this.CustomProperties = new NameValueCollection();
    }

    protected override bool OnDeserializeUnrecognizedElement(string elementName, XmlReader reader)
    {
        this.CustomProperties.Add(elementName, reader.ReadString());
        return true;
    }
}

I had to solve the same issue.

like image 151
frantisek Avatar answered Oct 30 '22 20:10

frantisek