Helo
Can anybody explain me how to get configuration element from .config file. I know how to handle attributes but not elements. As example, I want to parse following:
<MySection enabled="true">
<header><![CDATA[ <div> .... </div> ]]></header>
<title> .... </title>
</MySection>
My c# code looks like this so far:
public class MyConfiguration : ConfigurationSection
{
[ConfigurationProperty("enabled", DefaultValue = "true")]
public bool Enabled
{
get { return this["enabled"].ToString().ToLower() == "true" ? true : false; }
}
[ConfigurationProperty("header")]
public string header
{
???
}
}
It works with attributes, how do I do with elements (header property in above code) ?
The ConfigurationElement is an abstract class that is used to represent an XML element in a configuration file (such as Web. config). An element in a configuration file can contain zero, one, or more child elements. Because the ConfigurationElement class is defined as abstract, you cannot create an instance of it.
Configuration Elements allow you to create defaults and variables to be used by Samplers. They are used to add or modify requests made by Samplers. They are executed at the start of the scope of which they are part, before any Samplers that are located in the same scope.
There is another approach for doing the same thing.
We could create an element by overriding DeserializeElement
method to get string value:
public class EmailTextElement : ConfigurationElement {
public string Value { get; private set; }
protected override void DeserializeElement(XmlReader reader, bool s) {
Value = reader.ReadElementContentAs(typeof(string), null) as string;
}
}
Here's a pretty good custom config section designer tool you can use (and it's free):
Configuration Section Designer
EDIT:
I was looking into MSDN and it seems that custom config sections can't do what you want, ie. getting the config value from an element. Custom config elements can contain other config elements, but the config values always come from attributes.
Maybe you can put your html snippets into other files and refer to them from the config, like this.
<MySection enabled="true">
<header filename="myheader.txt" />
<title filename="mytitle.txt" />
</MySection>
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