How to get configuration element

别等时光非礼了梦想. 提交于 2019-12-01 05:20:38
Vikash Kumar

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>

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

Working with your example, you are going to override the Deserialization of "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();
      }
    }

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

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.

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.

According to MSDN, in .NET 4 there's a new CurrentConfiguration property which gives you a reference to the top-level Configuration instance that represents the configuration hierarchy that the current ConfigurationElement instance belongs to.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!