2011-11-25 192 views
6

我在閱讀'App.config'中的設置。我只是想出瞭如何使用ConfigurationSectionConfigurationElementCollectionConfigurationelElement爲什麼我不能將屬性轉換爲嵌套元素?

的App.config:

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <configSections> 
     <sectionGroup name="notificationSettingsGroup"> 
       <section name="mailTemplates" type="Project.Lib.Configuration.MailTemplateSection, Project.Lib" 
        allowDefinition="Everywhere" allowExeDefinition="MachineToApplication" requirePermission="false"/> 
     </sectionGroup>   
    </configSections> 
    <notificationSettingsGroup> 
     <mailTemplates> 
      <items> 
       <mailTemplate name="actionChain" subject="Subject bla-bla"> 
        <body>Body bla-bla</body> 
       </mailTemplate>     
      </items> 
     </mailTemplates> 
    </notificationSettingsGroup> 
    <startup> 
     <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/> 
    </startup> 
</configuration> 

我的C#代碼:

public class MailTemplateSection : ConfigurationSection 
{ 
    [ConfigurationProperty("items", IsDefaultCollection = false)] 
    public MailTemplateCollection MailTemplates 
    { 
     get { return (MailTemplateCollection)this["items"]; } 
     set { this["items"] = value; } 
    } 
} 

[ConfigurationCollection(typeof(MailTemplateElement), AddItemName = "mailTemplate", 
    CollectionType = ConfigurationElementCollectionType.AddRemoveClearMap)] 
public class MailTemplateCollection : ConfigurationElementCollection 
{ 
    protected override ConfigurationElement CreateNewElement() 
    { 
     return new MailTemplateElement(); 
    } 

    protected override object GetElementKey(ConfigurationElement element) 
    { 
     return ((MailTemplateElement) element).Name; 
    } 
} 

public class MailTemplateElement : ConfigurationElement 
{ 
    [ConfigurationProperty("name", DefaultValue = "action", IsKey = true, IsRequired = true)] 
    public string Name 
    { 
     get { return (string)this["name"]; } 
     set { this["name"] = value; } 
    } 

    [ConfigurationProperty("subject", DefaultValue = "Subject", IsKey = false, IsRequired = true)] 
    public string Subject 
    { 
     get { return (string)this["subject"]; } 
     set { this["subject"] = value; } 
    } 

    [ConfigurationProperty("body", DefaultValue = "Body", IsKey = false, IsRequired = true)] 
    public string Body 
    { 
     get { return (string)this["body"]; } 
     set { this["body"] = value; } 
    } 
} 

和工作代碼:

class Program 
{ 
    static void Main(string[] args) 
    { 
     Configuration config = 
      ConfigurationManager.OpenExeConfiguration(
      ConfigurationUserLevel.None); 

     var mailTemplatesSection = 
      config.GetSection("notificationSettingsGroup/mailTemplates") as MailTemplateSection; 

    } 
} 

所有作品,當我聲明瞭字段作爲XML屬性。但是,當我嘗試將屬性轉換爲嵌套元素 - 「Property'Body'不是ConfigurationElement」錯誤發生。

我在做什麼錯?

+0

請參閱此[在自定義配置中如何使用文本元素而不是屬性](http://stackoverflow.com/questions/5078758/can-i-add-a-textnode-instead-of-an-attribute-in -a-net-configurationsection) –

回答

3

因爲您必須創建自定義類型並從ConfigurationElement派生它們才能將它們用作配置文件中的元素。所有簡單類型總是寫爲屬性。 例如:

public class Body : ConfigurationElement 
{ 
    [ConfigurationProperty("value", DefaultValue = "Body", IsKey = true, IsRequired = true)] 
    public string Value{get;set;} 
} 

這將允許你寫

<body value="some val"/> 

在你的配置。

+0

Vladimir,我想將_large text_存儲在「body」屬性中,然後不想將此數據存儲在屬性中。恕我直言,大數據必須存儲在標籤內。 – lewis

+3

大文本不應該存儲在App.config中。使用數據庫或自定義格式。它可能很容易就是一個XmlSerializer生成的格式,爲此您可以創建類並完全控制數據序列化的方式。 –

相關問題