Why can't I convert an attribute to a nested element?

I read the settings from "App.config". I just figured out how to work with ConfigurationSection , ConfigurationElementCollection and 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> 

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

And the working code:

 class Program { static void Main(string[] args) { Configuration config = ConfigurationManager.OpenExeConfiguration( ConfigurationUserLevel.None); var mailTemplatesSection = config.GetSection("notificationSettingsGroup/mailTemplates") as MailTemplateSection; } } 

Everything works when I declare fields as attributes in xml. But when I try to convert attributes to a nested element, an error occurs. The "Property" property is not a ConfigurationElement element.

What am I doing wrong?

+6
source share
1 answer

Because you need to create your own types and derive them from ConfigurationElement in order to use them as elements in a configuration file. All simple types are always written as attributes. For instance:

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

This will allow you to write

 <body value="some val"/> 

in your configuration.

+3
source

Source: https://habr.com/ru/post/902387/


All Articles