Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Freeform XML configuration section body in app.config

Is there a way to make a configuration section that would allow a freeform XML body? How would I get that freeform body in code?

For example I'd like to create a ModuleConfigurationSection like this:

<modules>
    <module name="ModuleA" type="My.Namespace.ModuleA, My.Assembly">
        <moduleConfig>
            <serviceAddress>http://myserver/myservice.svc</serviceAddress>
        </moduleConfig>
    </module>
    <module name="ModuleB" type="My.Namespace.ModuleB, My.OtherAssembly">
        <moduleConfig>
            <filePath>c:\directory</filePath>
        </moduleConfig>
    </module>
</modules>

So some code would spin up each of these module types from config sections using ConfigurationManager.GetSection("modules") and I'd like to pass the XML inside the moduleConfig element as an opaque configuration value to the constructor of the module class.

Any input appreciated!

like image 377
joshperry Avatar asked Jul 21 '10 20:07

joshperry


People also ask

How do I add a config section in app config?

We will start with creating an class that can store the instance settings (the <add> element), then we'll create a collection that can store our instances (the <instances> element) and then we'll create the class to manage the Configuration Section (the <sageCRM> element).

Where do I put configSections in web config?

Next we need to make the config file aware of the custom section by adding the section declaration to the configSections element in the Web. config or App. config file. If there is no configSections element, create one at the top of the file just after the opening configuration tag.

Which section in a web config file can be used to specify application level variables as key value pairs?

The key/value pairs specified in the <appSettings> element are accessed in code using the ConfigurationSettings class. You can use the file attribute in the <appSettings> element of the Web. config and application configuration files.

What is configSections in app config?

<configSections> element for <configuration>Contains configuration section and namespace declarations.


2 Answers

This is how I ended up accomplishing this:

public class ModuleElement : ConfigurationElement
{
    [ConfigurationProperty("name", IsRequired = true)]
    public string Name
    {
        get { return (string)base["name"]; }
        set { base["name"] = value; }
    }

    XElement _config;
    public XElement Config
    {
        get { return _config;  }
    }

    protected override bool OnDeserializeUnrecognizedElement(string elementName, System.Xml.XmlReader reader)
    {
        if (elementName == "config")
        {
            _config = (XElement)XElement.ReadFrom(reader);
            return true;
        }
        else
            return base.OnDeserializeUnrecognizedElement(elementName, reader);
    }
}

So the xml would look like:

<module name="ModuleA">
    <config>
        <filePath>C:\files\file.foo</filePath>
    </config>
</module>

The body of the config element could be any freeform xml that you like. Assuming that you set up a collection, when you do ConfigurationManager.GetSection("modules") you can then access the Config property of each ModuleElement object as an XElement representing the XML of the config element node.

like image 123
joshperry Avatar answered Sep 20 '22 09:09

joshperry


In my application I could not use the .NET 3.5 Framework. I used a slightly different approach and came up with this piece of code:

public class ModuleSection : ConfigurationSection
{
    private const string ELEMENT_NAME_CONFIG = "config";

    private XmlNode _configNode;

    [ConfigurationProperty("name", IsRequired = true)]
    public string Name
    {
        get { return (string)base["name"]; }
        set { base["name"] = value; }
    }

    public XmlNode Config
    {
        get { return _configNode; }
    }

    protected override bool OnDeserializeUnrecognizedElement(string elementName, XmlReader reader)
    {
        if(elementName.Equals(ELEMENT_NAME_CONFIG, StringComparison.Ordinal)) {
            // Add the unrecognized element.
            _configNode = _xmlDocument.ReadNode(reader);
            return true;
        } else {
            return base.OnDeserializeUnrecognizedElement(elementName, reader);
        }
    }
}
like image 39
mathijsuitmegen Avatar answered Sep 19 '22 09:09

mathijsuitmegen