2011-11-25 9 views
6

मैं 'App.config' से सेटिंग्स पढ़ रहा हूं। मैंने अभी पता लगाया है कि ConfigurationSection, ConfigurationElementCollection और ConfigurationelElement के साथ कैसे काम करना है।मैं विशेषता को नेस्टेड तत्व में क्यों परिवर्तित नहीं कर सकता?

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> 

मेरे सी # कोड:

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 की विशेषताओं के रूप क्षेत्रों की घोषणा कर रहा हूँ। लेकिन जब मैं नेस्टेड तत्व में गुणों को रूपांतरित करने का प्रयास करता हूं - "संपत्ति 'बॉडी' कॉन्फ़िगरेशन एलिमेंट नहीं है" त्रुटि होती है।

मैं क्या गलत कर रहा हूं?

+0

इसे देखें [कस्टम कॉन्फ़िगरेशन में विशेषता के बजाय टेक्स्ट तत्व का उपयोग कैसे करें] (http://stackoverflow.com/questions/5078758/can-i-add-a-textnode-instead-of-an-attribute-in -ए-नेट-कॉन्फ़िगरेशनसेक्शन) –

उत्तर

3

क्योंकि आपको कस्टम प्रकार बनाना है और उन्हें कॉन्फ़िगरेशन फ़ाइल में तत्वों के रूप में उपयोग करने के लिए उन्हें कॉन्फ़िगरेशन एलिमेंट से प्राप्त करना है। सभी सरल प्रकार हमेशा गुणों के रूप में लिखे जाते हैं। उदाहरण के लिए:

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

यह आपको अपने config में

<body value="some val"/> 

लिखने के लिए अनुमति देगा।

+0

व्लादिमीर, मैं "बॉडी" विशेषता में _large text_ स्टोर करना चाहता हूं, फिर इस डेटा को विशेषता में स्टोर नहीं करना चाहता। आईएमएचओ, टैग के अंदर बड़े डेटा को संग्रहीत किया जाना चाहिए। – lewis

+3

बड़े ग्रंथों को App.config में बिल्कुल भी संग्रहित नहीं किया जाना चाहिए। या तो डेटाबेस, या कस्टम प्रारूप का प्रयोग करें। यह आसानी से XmlSerializer जेनरेटेड प्रारूप हो सकता है, जिसके लिए आप कक्षाएं बना सकते हैं और पूरी तरह से नियंत्रित कर सकते हैं कि आपका डेटा क्रमबद्ध कैसे है। –

संबंधित मुद्दे

 संबंधित मुद्दे