【问题标题】:How to get configuration element如何获取配置元素
【发布时间】:2010-10-27 14:38:57
【问题描述】:

直升机

谁能解释我如何从 .config 文件中获取配置元素。 我知道如何处理属性而不是元素。例如,我想解析以下内容:

<MySection enabled="true">

 <header><![CDATA[  <div> .... </div>  ]]></header>

 <title> .... </title>

</MySection>

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

 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
        {
                ???
        }
  }

它适用于属性,我如何处理元素(上面代码中的标题属性)?

【问题讨论】:

    标签: c# configuration web-config app-config


    【解决方案1】:

    还有另一种方法可以做同样的事情。

    我们可以通过重写DeserializeElement方法来创建一个元素来获取字符串值:

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

    【讨论】:

    • 最好使用reader.ReadElementContentAsString() API。
    【解决方案2】:

    这是一个非常好的自定义配置部分设计器工具,您可以使用(而且它是免费的):

    Configuration Section Designer

    编辑:

    我正在研究 MSDN,似乎自定义配置部分无法满足您的需求,即。从元素获取配置值。自定义配置元素可以包含其他配置元素,但配置值始终来自属性。

    也许您可以将您的 html sn-ps 放入其他文件并从配置中引用它们,就像这样。

    <MySection enabled="true"> 
      <header filename="myheader.txt" />
      <title filename="mytitle.txt" />
    </MySection>
    

    【讨论】:

    • 对我来说是丑陋的解决方案。您想以这种方式设置 html 页面标题和页眉吗? :) 我不会,尤其是因为它只有 1 行或几行(html)行。这消除了场景中的属性,因为用户不能使用 CDATA 来设置 html 字符串。
    • 好吧,那么你必须使用自定义配置文件和自定义解析。
    • Vizu,1 票投给配置部分设计器链接。在那里为我节省了很多无聊的编码。谢谢
    【解决方案3】:

    继承 ConfigurationElement 类并重写其反序列化方法。使用新类来表示具有文本内容的元素。

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

    【讨论】:

      【解决方案4】:

      使用您的示例,您将覆盖 ConfigurationElement 中“header”的反序列化以获取 CDATA 值。

      <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】:

        您可以使用ConfigurationManager.GetSection("SectionName") 方法获取配置文件中的配置部分。

        【讨论】:

        • 错了。问题是关于设计自定义配置部分的热点。
        【解决方案6】:

        我终于找到了一种方法。

        有 IConfigurationSectionHandler 接口允许我想要的东西。它需要编写方法的人

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

        之后,你自己解析 section,这样我就可以毫无问题地获取 XmlElement:

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

        这样做的缺点是接口已经过时,但 MSDN 声明它不会从框架的未来版本中删除,因为它在内部使用。

        【讨论】:

        • 您没有指定目标的 .Net fw 版本。对于 .Net 1.1 以上的所有版本,important remark 应注明:IConfigurationSectionHandler 在 .NET Framework 2.0 及更高版本中已弃用。
        【解决方案7】:

        您可以创建一个继承自 System.Configuration.ConfigurationElement 的类,该类表示您的配置部分中的一个元素。

        the MSDN documentation for ConfigurationElement中有一个简单的例子。

        【讨论】:

          【解决方案8】:

          根据MSDN,在.NET 4 中有一个新的CurrentConfiguration 属性,它为您提供对顶级Configuration 实例的引用,该实例表示当前@ 的配置层次结构987654324@实例所属。

          【讨论】:

            猜你喜欢
            • 2011-05-08
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-02-26
            • 2016-11-09
            • 2015-01-04
            • 1970-01-01
            • 2012-01-23
            相关资源
            最近更新 更多