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!
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).
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.
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.
<configSections> element for <configuration>Contains configuration section and namespace declarations.
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);
}
}
}
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