Solved

How do I nest multiple nested config sections

Posted on 2011-02-24
3
628 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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Error handling in asp.net site 5 28
InputLanguage 1 27
get row value in vb.net 4 14
Call Controller Action Method from ASPX 2 15
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
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…

803 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