How to get configuration element

2019-02-17 10:47发布

问题:

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

回答1:

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

}


回答2:

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>


回答3:

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



回答4:

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



回答5:

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



回答6:

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.



回答7:

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.



回答8:

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.