2009-05-20 75 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(); 
 
     } 
 
    }