1
我有一个自定义的配置设置。从web.config中提取xml元素的列表
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"]; }
}
}
在web.config文件中添加一个包含此xml配置的节。
<resizeOptions>
<add name="Square" width="100" height="100" />
<add name="Rectangle" width="200" height="100" />
<add name="Hero" width="600" height="400" />
</resizeOptions>
我留下了很多关于简洁的其他代码出来的,但我想我已经提供了需要问我的问题一切。
如何更改GalleryResizeOptionsCollection,以便我可以返回所有GalleryResizeOptionsElements的列表?或者更清楚地说,我希望能够返回所有“添加”元素的列表。
看到http://stackoverflow.com/questions/2260317/change-a-web-config-programmatically-with-c-sharp-净和http://stackoverflow.com/questions/4357238/is-there-a-way-to-programmatically-save-values-to-web-config-appsettings-无希望这会帮助你。 – 2012-03-10 04:07:07