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 6993373
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:45:23+00:00 2026-05-27T19:45:23+00:00

Im having problems accessing a proprties collection attribute name for my pages element. Pages

  • 0

Im having problems accessing a proprties collection attribute “name” for my pages element. Pages has a collection of page field that have attributes Could someone have a look at my code and show me how have a collection of pages with a name attribute on each one and access its value. At the moment my code return nothing but the page loads without any errors so I don’t know whats going on and how to get the attribute field.

<configSections>
  <sectionGroup name="site" type="MyProject.Configuration.Site">
    <section name="pages" type="MyProject.Configuration.Pages"/>      
  </sectionGroup>  
</configSections>


<site>
  <pages>
    <page name="test">        
    </page>
  </pages>    
</site>

Classes:

public class Site : ConfigurationSection
{
    [ConfigurationProperty("pages")]
    [ConfigurationCollection(typeof(PageCollection), AddItemName="page")]
    public PageCollection Pages
    {
        get
        {
            return base["pages"] as PageCollection;
        }
    }
}

public class PageCollection : ConfigurationElementCollection
{

    public PageCollection()
    {
        PageElement element = (PageElement)CreateNewElement();
        BaseAdd(element); // doesn't work here does if i remove it
    }

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

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

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

}

public class PageElement : ConfigurationElement
{
    public PageElement() { }

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

Code to access my attribute:

Pages pageSettings = ConfigurationManager.GetSection("site/pages") as Pages;
lblPage.Text = pageSettings.Page[0].Name;
  • 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-27T19:45:23+00:00Added an answer on May 27, 2026 at 7:45 pm

    The issue is that your section element should be site, not pages:

    public class Site: ConfigurationSection
    {
        [ConfigurationProperty("pages")]
        public PageCollection Page
        {
            get
            {
                return base["pages"] as PageCollection;
            }
        }
    }
    

    Update

    It turns out that there were a few issues that needed to be addressed:

    1) The web.config needs to be changed to be a section instead of a sectiongroup and the pages element should be removed:

      <configSections>
        <section name="site" type="MyProject.Configuration.Site, MyProject.Configuration">
        </section>
      </configSections>
    

    2) The Site class needs to be modified:

    public class Site : ConfigurationSection
    {
        [ConfigurationProperty("pages")]
        [ConfigurationCollection(typeof(PageCollection), AddItemName="page")]
        public PageCollection pages
        {
            get
            {
                return base["pages"] as PageCollection;
            }
        }
    }
    

    3) The PageCollection constructor needs to have its code removed.

        public PageCollection()
        {
        }
    

    4) The name property needs to be removed from the PageCollection, or at least marked as not required.

    5) The call to retrieve the settings is now:

    Site site = ConfigurationManager.GetSection("site") as Site;
    

    I have tested this and verified that these changes will successfully read in the config.

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

Sidebar

Related Questions

I heard that some people where having problems accessing their sites which get their
I have created two Facebook canvas apps. I am having problems with people accessing
I am having problem in accessing user control properties from page. I have usercontrol
I have an old .net 2005 web site that has some asp pages and
I am having a problem accessing the @attribute section of my SimpleXML object. When
I am having a problem accessing the ViewData object through javascript. I have set
Having problems iterating. Problem has to do with const correctness, I think. I assume
Im having problems with classpaths. I have used them before with import but I'm
We are having problems accessing information in .net configuration files (such as app.config and
I am having problems with accessing individual structure elemnsts. How to output each structure

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.