Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get configuration element

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) ?

like image 297
majkinetor Avatar asked May 20 '09 11:05

majkinetor


People also ask

What is a Configuration element?

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.

What are config elements in Jmeter?

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.


2 Answers

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

}
like image 52
Vikash Kumar Avatar answered Oct 05 '22 23:10

Vikash Kumar


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>
like image 43
Vizu Avatar answered Oct 06 '22 00:10

Vizu