?
Solved

How do I nest multiple nested config sections

Posted on 2011-02-24
3
Medium Priority
?
650 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
2 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month14 days, 15 hours left to enroll

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