Solved

How do I nest multiple nested config sections

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

706 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now