2009-05-20 66 views
6

直升机如何获取配置元素

任何人都可以解释我如何从.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 
     { 
       ??? 
     } 
    } 

它的工作原理与属性,我该怎么办的元素(头属性在上面的代码中) ?

回答

0

我终于找到了一种方法来做到这一点。

有IConfigurationSectionHandler接口,允许我想要的东西。它要求一个写

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

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

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

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

4

这里是一个不错的自定义配置部分设计工具,您可以使用(而且是免费的):

Configuration Section Designer

编辑:

我一直在寻找到MSDN,似乎自定义配置部分可做你想做的事,即。从一个元素获取配置值。自定义配置元素可以包含其他配置元素,但配置值始终来自属性。

也许你可以把你的html代码片段放到其他文件中,并从配置文件中引用它们,就像这样。

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

对我来说丑陋的解决方案。你想以这种方式设置html页面标题和标题吗? :)我不会,特别是因为它将只有1或几(html)行。这消除了场景的属性,因为用户无法使用CDATA来设置html字符串。 – majkinetor 2009-05-20 13:19:12

+0

那么,你必须使用自定义分析的自定义配置文件。 – Vizu 2009-05-20 13:30:55

+0

Vizu,1个配置节设计器链接。在那里给了我很多无聊的编码。谢谢 – Liam 2010-06-10 07:49:14

0

MSDN,在.NET 4有一个新CurrentConfiguration属性,它使您能够代表当前ConfigurationElement实例所属的配置层次顶级Configuration实例的引用。

6

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

我们可以通过重写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; 
    } 

} 
1

你的榜样工作,你要重写“​​头”的反序列化中的ConfigurationElement获得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(); 
 
     } 
 
    }