[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

How do I nest multiple nested config sections

Posted on 2011-02-24
3
Medium Priority
?
647 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
[X]
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
3 Comments
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

656 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