开发者

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</serviceA开发者_JS百科ddress>
        </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!


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.


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);
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜