如何获得配置元素 [英] How to get configuration element

查看:136
本文介绍了如何获得配置元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

直升机

任何人都可以解释我如何从config文件获取配置元素。
我知道如何处理的属性而不是元素。作为例子,我想分析如下:

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>



我的C#代码如下所示到目前为止:

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

推荐答案

我终于找到了如何做这件事。

I finally found one way to do it.

有是IConfigurationSectionHandler接口,允许事情我想要的。它要求一个写方法

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)

这之后,U解析的部分的你自己,所以我能够获取的XmlElement的没有问题

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;



这样做的缺点是,接口是过时的,但MSDN说,它不会从将来被移除因为它是内部使用的框架版本。

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.

这篇关于如何获得配置元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆