views:

1438

answers:

6

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

A: 

You can use the ConfigurationManager.GetSection("SectionName") method for getting the configuration section in the config files.

Kirtan
+2  A: 

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>
Vizu
Ugly solution for me. Would you like to set up html page title and header that way ? :) I wouldn't, especially cuz its going to be only 1 or few (html) lines. This eliminates attributs from the scenario as user can't use CDATA with them to be able to set html string.
majkinetor
Well, then you have to use a custom config file with custom parsing.
Vizu
Vizu, 1 vote for the Configuration Section Designer link. Saved me a lot of boring coding there. Thanks
Liam
A: 

As an example,

Web.config

Code:

string connectionString = ConfigurationSettings.AppSettings["c"];
dasha salo
-1: The question is about custom config sections, not the appsetting stuff.
Vizu
A: 

I finally found one way to do it.

There is IConfigurationSectionHandler interface that allows for things I want. It requires the one to write the method

 public object Create(object parent, object configContext, XmlNode section)

After it, u parse section on your own so I was able to fetch XmlElement's without a problem:

        header  = s["header"]  != null ? s["header"].InnerText   : String.Empty;
        title   = s["title"]   != null ? s["title"].InnerText    : String.Empty;

The down side of this is that interface is outdated but MSDN states that it will not be removed from future versions of the frameworks as it is used internally.

majkinetor
A: 

You can create a class that inherits from System.Configuration.ConfigurationElement that represents an element in your configuration section.

There's a simple example in the MSDN documentation for ConfigurationElement.

Joe
A: 

Inherit the ConfigurationElement class and override its deserialize method. Use the new class to represent elements with text content.

http://www.codeproject.com/KB/XML/ConfigurationTextElement.aspx

ToddS