How to get a configuration item

Helo

Can someone explain to me how to get the configuration item from the .config file. I know how to handle attributes, but not elements. As an example, I want to analyze the following:

<MySection enabled="true"> <header><![CDATA[ <div> .... </div> ]]></header> <title> .... </title> </MySection> 

My C # code looks like this:

  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 should I do with elements (a property of the header in the above code)?

+6
c # web-config configuration app-config
source share
8 answers

I finally found one way to do this.

There is an IConfigurationSectionHandler interface that allows me to do what I want. To do this, write a method

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

After that, the u parse section is on its own, so I was able to get the XmlElement without problems:

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

The downside of this is that the interface is deprecated, but MSDN states that it will not be removed from future versions of the framework, as it is used internally.

0
source share

There is another approach to doing the same.

We could create an element by overriding the DeserializeElement method to get a 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; } } 
+6
source share

Here is a pretty good partition setting tool in the configuration section that you can use (and for free):

Configuration Section Constructor

EDIT:

I was looking for MSDN and it seems that custom configuration sections cannot do what you want, i.e. Getting the configuration value from the item Custom configuration items may contain other configuration items, but configuration values ​​always come from attributes.

Perhaps you can put your html fragments in other files and refer to them from the configuration, for example.

 <MySection enabled="true"> <header filename="myheader.txt" /> <title filename="mytitle.txt" /> </MySection> 
+4
source share

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

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

+3
source share

Working with your example will override the deserialization of the header in the ConfigurationElement to get the CDATA value.

 <MySection enabled="true"> <header name="foo"><![CDATA[ <div> .... </div> ]]></header> <title> .... </title> </MySection> 

  public sealed class HeaderSection: ConfigurationElement { private string __Name, __CDATA; [ConfigurationProperty("name", IsRequired = true)] public string Name { get { return this.__Name; } set { this.__Name = value; } } [ConfigurationProperty("value", IsRequired = true)] public string Value { get { return this.__CDATA; } set { this.__CDATA = value; } } protected override void DeserializeElement(System.Xml.XmlReader reader, bool s) { this.Name = reader.GetAttribute("name").Trim(); string cdata = reader.ReadElementContentAs(typeof(string), null) as string; this.Value = cdata.Trim(); } } 
+1
source share

You can use ConfigurationManager.GetSection ("SectionName") to get the configuration section in the configuration files.

0
source share

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

Here is a simple example in the MSDN documentation for ConfigurationElement .

0
source share

According to MSDN , .NET 4 introduced a new CurrentConfiguration property that gives you a link to a top-level Configuration instance representing the configuration hierarchy to which the current ConfigurationElement instance belongs.

0
source share

All Articles