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>
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With