Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How do I nest multiple nested config sections

Posted on 2011-02-24
3
629 Views
Last Modified: 2012-08-14
Im creating a custom configuration in a console application and am wondering if a configuration of the following format is even possible.
 
<CustomSettings>
	<EmailSettings>
		<SmtpServer>mx1.myemailserver.com</SmtpServer>
		<SmtpUserName>notifications@myemail.com</SmtpUserName>
		<SmtpPassword></SmtpPassword>
		<SmtpPort>25</SmtpPort>
		<SmtpUseSSL>false</SmtpUseSSL>
		<EmailLists>
			<List name="Administrators">
				<Recipient>me@myemail.com</Recipient>
				<Recipient>you@myemail.com</Recipient>
			</List>
			<List name="TransactionRecipients">
				<Recipient>me@myemail.com</Recipient>
			</List>
		</EmailLists>
	</EmailSettings>
</CustomSettings>

Open in new window

I am able to get the configurationElements of List but how do I get the child collection of Recipients

All Code so far is below.
 
public class CustomSettings : ConfigurationSection
    {
        [ConfigurationProperty("EmailSettings")]
        public EmailSettings EmailSettings
        {
            get
            {
                return (EmailSettings)this["EmailSettings"];
            }
        }
    }

public class EmailSettings : ConfigurationElement
    {
        [ConfigurationProperty("SmtpServer", IsRequired = true)]
        public ConfigurationTextElement<String> SmtpServer
        {
            get { return (ConfigurationTextElement<String>)this["SmtpServer"]; }
            set { this["SmtpServer"] = value; }
        }

        [ConfigurationProperty("SmtpUserName", IsRequired = true)]
        public ConfigurationTextElement<String> SmtpUserName
        {
            get { return (ConfigurationTextElement<String>)this["SmtpUserName"]; }
            set { this["SmtpUserName"] = value; }
        }

        [ConfigurationProperty("SmtpPassword", IsRequired = false)]
        public ConfigurationTextElement<String> SmtpPassword
        {
            get { return (ConfigurationTextElement<String>)this["SmtpPassword"]; }
            set { this["SmtpPassword"] = value; }
        }

        [ConfigurationProperty("SmtpPort", IsRequired = false)]
        public ConfigurationTextElement<int> SmtpPort
        {
            get { return (ConfigurationTextElement<int>)this["SmtpPort"]; }
            set { this["SmtpPort"] = value; }
        }

        [ConfigurationProperty("SmtpUseSSL", IsRequired = false)]
        public ConfigurationTextElement<bool> SmtpUseSSL
        {
            get { return (ConfigurationTextElement<bool>)this["SmtpUseSSL"]; }
            set { this["SmtpUseSSL"] = value; }
        }

        [ConfigurationProperty("EmailLists")]
        [ConfigurationCollection(typeof(EmailListElement), AddItemName = "List")]
        public EmailLists EmailLists
        {
            get { return (EmailLists)this["EmailLists"]; }
        }
    }

public class EmailLists : ConfigurationElementCollection
    {
        public override ConfigurationElementCollectionType CollectionType
        {
            get { return base.CollectionType; }
        }

        protected override ConfigurationElement CreateNewElement()
        {
            return new EmailListElement();
        }

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

        protected override string ElementName
        {
            get { return "List"; }
        }

        public new int Count
        {
            get { return base.Count; }
        }

        public EmailListElement this[int index]
        {
            get { return (EmailListElement)BaseGet(index); }
            set
            {
                if (BaseGet(index) != null)
                    BaseRemoveAt(index);
                BaseAdd(index, value);
            }
        }

        new public EmailListElement this[string Name]
        {
            get { return (EmailListElement)BaseGet(Name); }
        }

        public int IndexOf(EmailListElement list)
        {
            return BaseIndexOf(list);
        }

        protected override void BaseAdd(ConfigurationElement element)
        {
            BaseAdd(element, false);
        }
    }

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

Open in new window


I would rather not have to wrap the Recipient list with a container such as
<Recipients>
    <Recipient>...</Recipient>
    <Recipient>...</Recipient>
</Recipients>

I would think I would be able to do something like EmailListElement.Recipients

Thanks for any help
0
Comment
Question by:wilks01
3 Comments
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 500 total points
ID: 35021435
You can try by changing these

<List name="Administrators">
                        <Recipient>me@myemail.com</Recipient>
                        <Recipient>you@myemail.com</Recipient>

to

<List name="Administrators" Emails="me@myemail.com;you@myemail.com">

Then split the Emails on ;
0
 
LVL 19

Expert Comment

by:Amandeep Singh Bhullar
ID: 35913568
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question