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

  • Home
  • SEARCH
  • 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 7081697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:57:12+00:00 2026-05-28T06:57:12+00:00

I’m having trouble getting a custom config section to work. It’s some code I

  • 0

I’m having trouble getting a custom config section to work. It’s some code I got from the web in an effort to try to understand this area a little better and enable me to get to where I want to ultimatly be, my own custom config section.

The error I get when I run the code in a console app is
‘
Unrecognized attribute ‘extension’. Note that attribute names are case-sensitive.‘

The code in the main app to get things going is

var conf = ConfigurationManager.GetSection("uploadDirector");

and this is where the exception appears.

This is the config section I am hoping/trying to achieve

<AuthorisedClients>
    <AuthorisedClient name="Client">
      <Queue id="1" />
      <Queue id="7" />
    </AuthorisedClient>
    <AuthorisedClient name="Client2">
      <Queue id="3" />
      <Queue id="4" />
    </AuthorisedClient>
  </AuthorisedClients>

Here’s the code I have got from the web

.config file

<uploadDirector>
    <filegroup name="documents" defaultDirectory="/documents/">
      <clear/>
      <add extension="pdf" mime="application/pdf" maxsize="100"/>
      <add extension="doc" mime="application/word" maxsize="500"/>
    </filegroup>
    <filegroup name="images">
      <clear/>
      <add extension="gif" mime="image/gif" maxsize="100"/>
    </filegroup>
  </uploadDirector>

UploadDirectorConfigSection.cs

public class UploadDirectorConfigSection : ConfigurationSection {

        private string _rootPath;

        public UploadDirectorConfigSection() {

        }

        [ConfigurationProperty("rootpath", DefaultValue="/", IsRequired=false, IsKey=false)]
        [StringValidator(InvalidCharacters=@"~!.@#$%^&*()[]{};'\|\\")]
        public string RootPath {
            get { return _rootPath; }
            set { _rootPath = value; }
        }

        [ConfigurationProperty("", IsRequired = true, IsKey = false, IsDefaultCollection = true)]
        public FileGroupCollection FileGroups {
            get { return (FileGroupCollection) base[""]; }
            set { base[""] = value; }
        }
    }

FileGroupCollection.cs

public class FileGroupCollection : ConfigurationElementCollection {
        protected override ConfigurationElement CreateNewElement() {
            return new FileGroupElement();
        }

        protected override object GetElementKey(ConfigurationElement element) {
            return ((FileGroupElement) element).Name;
        }

        public override ConfigurationElementCollectionType CollectionType {
            get {
                return ConfigurationElementCollectionType.BasicMap;
            }
        }

        protected override string ElementName {
            get {
                return "filegroup";
            }
        }

        protected override bool IsElementName(string elementName) {
            if (string.IsNullOrWhiteSpace(elementName) || elementName != "filegroup")
                return false;
            return true;
        }

        public FileGroupElement this[int index] {
            get { return (FileGroupElement) BaseGet(index); }
            set {
                if(BaseGet(index) != null)
                    BaseRemoveAt(index);
                BaseAdd(index, value);
            }
        }
    }

FileGroupElement.cs

public class FileGroupElement : ConfigurationElement {

        [ConfigurationProperty("name", IsKey=true, IsRequired = true)]
        [StringValidator(InvalidCharacters = @" ~.!@#$%^&*()[]{}/;'""|\")]
        public string Name {
            get { return (string) base["name"]; }
            set { base["name"] = value; }
        }

        [ConfigurationProperty("defaultDirectory", DefaultValue = ".")]
        public string DefaultDirectory {
            get { return (string) base["Path"]; }
            set { base["Path"] = value; }
        }

        [ConfigurationProperty("", IsDefaultCollection = true, IsRequired = true)]
        public FileInfoCollection Files {
            get { return (FileInfoCollection) base[""]; }
        }
    }

FileInfoCollection.cs

public class FileInfoCollection : ConfigurationElementCollection {
        protected override ConfigurationElement CreateNewElement() {
            return new FileInfoCollection();
        }

        protected override object GetElementKey(ConfigurationElement element) {
            return ((FileInfoElement) element).Extension;
        }
    }

FileInfoElement.cs

public class FileInfoElement : ConfigurationElement {

        public FileInfoElement() {
            Extension = "txt";
            Mime = "text/plain";
            MaxSize = 0;
        }

        [ConfigurationProperty("extension", IsKey = true, IsRequired = true)]
        public string Extension {
            get { return (string)base["extension"]; }
            set { base["extension"] = value; }
        }

        [ConfigurationProperty("mime", DefaultValue = "text/plain")]
        public string Mime {
            get { return (string) base["mime"]; }
            set { base["mime"] = value; }
        }

        [ConfigurationProperty("maxsize", DefaultValue = 0)]
        public int MaxSize {
            get { return (int) base["maxsize"]; }
            set { base["maxsize"] = value; }
        }
    }
  • 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-05-28T06:57:12+00:00Added an answer on May 28, 2026 at 6:57 am

    In your definition of FileInfoCollection in the method CreateNewElement you create FileInfoCollection which is wrong. Overridden CreateNewElement should return new collection element, not the new collection:

    public class FileInfoCollection : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new FileInfoElement();
        }
    
        protected override object GetElementKey (ConfigurationElement element)
        {
            return ((FileInfoElement)element).Extension;
        }
    }
    

    Regarding your desired configuration, probably the simplest implementation will look like:

    public class AuthorisedClientsSection : ConfigurationSection {
        [ConfigurationProperty("", IsDefaultCollection = true)]
        public AuthorisedClientElementCollection Elements {
            get { return (AuthorisedClientElementCollection)base[""];}
        }
    }
    
    public class AuthorisedClientElementCollection : ConfigurationElementCollection {
        const string ELEMENT_NAME = "AuthorisedClient";
    
        public override ConfigurationElementCollectionType CollectionType {
            get { return ConfigurationElementCollectionType.BasicMap; }
        }
    
        protected override string ElementName {
            get { return ELEMENT_NAME; }
        }
    
        protected override ConfigurationElement CreateNewElement() {
            return new AuthorisedClientElement();
        }
    
        protected override object GetElementKey(ConfigurationElement element) {
            return ((AuthorisedClientElement)element).Name;
        }
    }
    
    public class AuthorisedClientElement : ConfigurationElement {
        const string NAME = "name";
    
        [ConfigurationProperty(NAME, IsRequired = true)]
        public string Name {
            get { return (string)base[NAME]; }
        }
    
        [ConfigurationProperty("", IsDefaultCollection = true)]
        public QueueElementCollection Elements {
            get { return (QueueElementCollection)base[""]; }
        }
    }
    
    public class QueueElementCollection : ConfigurationElementCollection {
        const string ELEMENT_NAME = "Queue";
    
        public override ConfigurationElementCollectionType CollectionType {
            get { return ConfigurationElementCollectionType.BasicMap; }
        }
    
        protected override string ElementName {
            get { return ELEMENT_NAME; }
        }
    
        protected override ConfigurationElement CreateNewElement() {
            return new QueueElement();
        }
    
        protected override object GetElementKey(ConfigurationElement element) {
            return ((QueueElement)element).Id;
        }
    }
    
    public class QueueElement : ConfigurationElement {
        const string ID = "id";
    
        [ConfigurationProperty(ID, IsRequired = true)]
        public int Id {
            get { return (int)base[ID]; }
        }
    }
    

    And the test:

    var authorisedClientsSection = ConfigurationManager.GetSection("AuthorisedClients")
        as AuthorisedClientsSection;
    
    foreach (AuthorisedClientElement client in authorisedClientsSection.Elements) {
        Console.WriteLine("Client: {0}", client.Name);
    
        foreach (QueueElement queue in client.Elements) {
            Console.WriteLine("\tQueue: {0}", queue.Id);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I've got a string that has curly quotes in it. I'd like to replace
Seemingly simple, but I cannot find anything relevant on the web. What is the
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

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.