Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7892311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:50:19+00:00 2026-06-03T06:50:19+00:00

I’ve wanted to persist some data between application sessions and decided to use the

  • 0

I’ve wanted to persist some data between application sessions and decided to use the .NET Configuration API (System.Configuration namespace) which has proven to be far more difficult than I expected.

I’ve created a custom configuration section as so:

UserMailSourcesSection

public class UserMailSourcesSection : ConfigurationSection
{
    [ConfigurationProperty("User", IsRequired = true)]
    public string User { get; set; }

    [ConfigurationProperty("UserMailSources", IsDefaultCollection = false)]
    [ConfigurationCollection(typeof(MailSourceElemet))]
    public MailSourceElemetCollection MailSources
    {
        get
        {
            return (MailSourceElemetCollection)this["UserMailSources"];
        }

        set
        {
            this["UserMailSources"] = value;
        }
    }

    public UserMailSourcesSection()
    {
        this.User = String.Empty;
    }
}

(The rest of the relevant code is at the end of the question)

Now the actual problem was that I was getting exceptions (not very descriptive ones, mind you) from the part of my code that saved this section after it’s been configured with all the values I wanted to persist. I traced it down to the MailSourceElementCollection.Add being called with a parameter that had some null values. That parameter was being passed from Microsofts code. I deduced that it must be creating a new instance of the parameter with the default parameter-less constructor.
I though the solution would be to explicitly write a parameter-less constructor that would replace the null values with some kind of defaults (specifically String.Empty) and that seemed to work. The code ran, the save method didn’t throw but there was a new problem – no data was actually being saved even though I explicitly set everything in the Main method:

PublisherElemet publisher = new PublisherElemet();
publisher.Publisher = "test";

PublisherElementCollection publishers = new PublisherElementCollection();
publishers.Add(publisher);
MailSourceElemet mailSource = new MailSourceElemet();
mailSource.HostName = "test";
mailSource.Port = 0;
mailSource.Username = "test";
mailSource.Password = "test";
mailSource.Publishers = publishers;

MailSourceElemetCollection mailSources = new MailSourceElemetCollection();
mailSources.Add(mailSource);

UserMailSourcesSection userMailSources = new UserMailSourcesSection();
userMailSources.User = Environment.UserName;
userMailSources.MailSources = mailSources;

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.Sections.Add("MailSources", userMailSources);
config.Save(ConfigurationSaveMode.Modified);

After this the expected output was:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
 <configSections>
  <section name="MailSources" type="Email_To_Rss_V2.Classes.UserMailSourcesSection, Email-To-Rss-V2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
 </configSections>
 <MailSources User="User">
  <UserMailSources>
    <add HostName="test" Port="0" Username="test" Password="test">
      <Publishers>
        <add Publihser="test" />
      </Publishers>
    </add>
  </UserMailSources>
</MailSources>

While the actual output was:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
 <configSections>
  <section name="MailSources" type="Email_To_Rss_V2.Classes.UserMailSourcesSection, Email-To-Rss-V2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
 </configSections>
 <MailSources User="">
  <UserMailSources>
    <add HostName="" Port="0" Username="" Password="">
      <Publishers>
        <add Publihser="" />
      </Publishers>
    </add>
  </UserMailSources>
</MailSources>

The really weird thing was that in the debugger the data was shown to be there, it just didn’t seem to output to file.

Here’s the rest of the relevant code:

MailSourceElementCollection

public class MailSourceElemetCollection : ConfigurationElementCollection
{
    public override ConfigurationElementCollectionType CollectionType
    {
        get
        {
            return ConfigurationElementCollectionType.AddRemoveClearMap;
        }
    }

    public MailSourceElemet this[int index]
    {
        get
        {
            return (MailSourceElemet)BaseGet(index);
        }

        set
        {
            if (BaseGet(index) != null)
            {
                BaseRemoveAt(index);
            }

            BaseAdd(0, value);
        }
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        if (((MailSourceElemet)element).Username == null)
        {
            Console.WriteLine("null");
        }

        return ((MailSourceElemet)element).Username;
    }

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

    public void Add(MailSourceElemet mailSource)
    {
        BaseAdd(mailSource);
    }

    public void Clear()
    {
        this.BaseClear();
    }

    public void Remove(MailSourceElemet element)
    {
        BaseRemove(element.Username);
    }

    public void Remove(string name)
    {
        BaseRemove(name);
    }

    public void RemoveAt(int index)
    {
        BaseRemoveAt(index);
    }
}

MailSourceElement

public class MailSourceElemet : ConfigurationElement
{
    [ConfigurationProperty("HostName", IsRequired = true)]
    public string HostName { get; set; }

    [ConfigurationProperty("Port", IsRequired = true)]
    public int Port { get; set; }

    [ConfigurationProperty("Username", IsRequired = true)]
    public string Username { get; set; }

    [ConfigurationProperty("Password", IsRequired = true)]
    public string Password { get; set; }

    [ConfigurationProperty("Publishers", IsDefaultCollection = false)]
    [ConfigurationCollection(typeof(PublisherElemet))]
    public PublisherElementCollection Publishers
    {
        get
        {
            return (PublisherElementCollection)this["Publishers"];
        }

        set
        {
            this["Publishers"] = value;
        }
    }

    public MailSourceElemet()
    {
        this.HostName = String.Empty;
        this.Port = 0;
        this.Username = String.Empty;
        this.Password = String.Empty;
    }
}

PublisherElementCollection

public class PublisherElementCollection : ConfigurationElementCollection
{
    public override ConfigurationElementCollectionType CollectionType
    {
        get
        {
            return ConfigurationElementCollectionType.AddRemoveClearMap;
        }
    }

    public PublisherElemet this[int index]
    {
        get
        {
            return (PublisherElemet)BaseGet(index);
        }

        set
        {
            if (BaseGet(index) != null)
            {
                BaseRemoveAt(index);
            }

            BaseAdd(0, (PublisherElemet)value);
        }
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((PublisherElemet)element).Publisher;
    }

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

    public void Add(PublisherElemet publisher)
    {
        BaseAdd(publisher);
    }

    public void Clear()
    {
        this.BaseClear();
    }

    public void Remove(PublisherElemet publisher)
    {
        BaseRemove(publisher.Publisher);
    }

    public void Remove(string name)
    {
        BaseRemove(name);
    }

    public void RemoveAt(int index)
    {
        BaseRemoveAt(index);
    }
}

PublisherElement

public class PublisherElemet : ConfigurationElement
{
    [ConfigurationProperty("Publihser", IsRequired = true)]
    public string Publisher { get; set; }

    public PublisherElemet()
    {
        Publisher = String.Empty;
    }
}

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T06:50:20+00:00Added an answer on June 3, 2026 at 6:50 am

    Have a look on the ‘Configuration Section Designer‘, this is an excellent
    ‘Visual Studio add-in that allows you to graphically design .NET Configuration Sections and automatically generates all the required code and a schema definition (XSD) for them’.

    Design you configuration section using this tool, [I think you will love it :)].

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I want to construct a data frame in an Rcpp function, but when I
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.