Demonstrating Custom Configuration for .NET (Part 2): Custom Elements
Allowing Richer Structures by Adding Custom Elements
Introduction
Previous Parts of This Series.
Part 1 kept things really simple. But for non-trivial custom configuration just having XML attributes to work with starts to get very hard to edit in the file. Rather one wants to employ the richer hierarchical structures that XML allows: child elements. This turns out to be very easy.
Creating A Custom Element
To keep things simple, this example will just change part 1’s structure:
into this:
All this takes is deriving a new class from System.Configuration.ConfigurationElement
with code attributes for the two configuration attributes, and then using this new
type as the type of the code property in the ConfigurationSection
derived
class. See the source code below: DataConfigurationElement
is the new custom element, and AppConfigSection
is the updated custom section,
now with just a single code property.
One limitation of this use of attributes is that text content is not supported
(<element>text content</element>
), custom elements still
just use attributes, or child custom elements (there does not seem to be any
depth limit). Unless one completely overrides the de-serialisation code
that makes custom elements easy (by overriding the
ConfigurationElement.DeserializeElement
method), but then all the work—including parsing the attributes—would
have to be manually coded using the passed XmlReader
instance.