2011-11-25 26 views
6

Tôi đang đọc cài đặt từ 'App.config'. Tôi chỉ tìm ra cách làm việc với ConfigurationSection, ConfigurationElementCollectionConfigurationelElement.Tại sao tôi không thể chuyển đổi thuộc tính thành phần tử lồng nhau?

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> 

My C# code:

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

Và mã làm việc:

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

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

    } 
} 

Tất cả các công việc, khi tôi tuyên bố trường là thuộc tính trong xml. Nhưng khi tôi cố gắng chuyển đổi các thuộc tính thành phần tử lồng nhau - "Thuộc tính" Nội dung "không phải là một ConfigurationElement" xảy ra lỗi.

Tôi đang làm gì sai?

+0

Tham khảo [cách sử dụng phần tử văn bản thay vì thuộc tính trong cấu hình tùy chỉnh] (http://stackoverflow.com/questions/5078758/can-i-add-a-textnode-instead-of-an-attribute-in -a-net-cấu hình) –

Trả lời

3

Vì bạn phải tạo các loại tùy chỉnh và lấy chúng từ ConfigurationElement để sử dụng chúng làm thành phần trong tệp cấu hình. Tất cả các loại đơn giản luôn được viết dưới dạng thuộc tính. Ví dụ:

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

này sẽ cho phép bạn viết

<body value="some val"/> 

trong cấu hình của bạn.

+0

Vladimir, tôi muốn lưu trữ _large text_ trong thuộc tính "body", sau đó không muốn lưu trữ dữ liệu này trong thuộc tính. IMHO, dữ liệu lớn phải được lưu trữ bên trong thẻ. – lewis

+3

Các văn bản lớn không nên được lưu trữ trong App.config. Sử dụng cơ sở dữ liệu hoặc định dạng tùy chỉnh. Nó có thể dễ dàng là một định dạng được tạo ra XmlSerializer, mà bạn có thể tạo các lớp và kiểm soát hoàn toàn cách dữ liệu của bạn được tuần tự hóa. –

Các vấn đề liên quan