1

I have a custom configuration setup.

public class GalleryResizeOptionsElement : ConfigurationElement
{
    [ConfigurationProperty("name")]
    public string Name
    {
        get { return (string)this["name"]; }
    }

    [ConfigurationProperty("width")]
    public int Width
    {
        get { return (int)this["width"]; }
    }

    [ConfigurationProperty("height")]
    public int Height
    {
        get { return (int)this["height"]; }
    }
}

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

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


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

    /// <summary>
    /// The element name of the configuration elements in the config file,
    /// as set at UserCommandConfigurationConstants.ElementName
    /// </summary>
    protected override string ElementName
    {
        get { return "add"; }
    }

    /// <summary>
    /// This is a convenience added to allow selection from the collection
    /// via the commandkey as an index
    /// </summary>
    /// <returns></returns>
    public GalleryResizeOptionsElement this[string name]
    {
        get { return (GalleryResizeOptionsElement)this.BaseGet(name); }
    }
}

public class GalleryConfigurationSection : ConfigurationSection
{

    public virtual GalleryResizeOptionsCollection ResizeOptions
    {
        get { return (GalleryResizeOptionsCollection) base["resizeOptions"]; }
    }
}

Add in the web.config file there is a section that contains this xml config.

<resizeOptions>
        <add name="Square" width="100" height="100" />
        <add name="Rectangle" width="200" height="100" />
        <add name="Hero" width="600" height="400" />
      </resizeOptions>

I've left a lot of the other code out for brevity, but I think I've included everything that is required to ask my question.

How do I alter GalleryResizeOptionsCollection so that I can return a list of all the GalleryResizeOptionsElements? Or in clearer terms, I want to be able to return a list of all the "add" elements.

Chris
  • 7,996
  • 11
  • 66
  • 98
  • see http://stackoverflow.com/questions/2260317/change-a-web-config-programmatically-with-c-sharp-net and http://stackoverflow.com/questions/4357238/is-there-a-way-to-programmatically-save-values-to-web-config-appsettings-without hope this will help you. – MANISHDAN LANGA Mar 10 '12 at 04:07

1 Answers1

0

You can use System.Linq to return a list of elements:

using System.Linq;

public class GalleryResizeOptionsCollection : ConfigurationElementCollection
{
   //...

   public IList<GalleryResizeOptionsElement> ToList()
   {
      return this.Cast<GalleryResizeOptionsElement>().ToList();
   }
}
KMoraz
  • 14,004
  • 3
  • 49
  • 82