Thanks for your response Carlo,
Seems something else cause this not work. Anyway, below is the complete
class code and ASP.NET webconfig setting for that custom section:
==========custom config classes============
namespace ConfigLib
{
public class UrlsSection : ConfigurationSection
{
public UrlsSection()
{
}
public UrlsSection(String attribVal)
{
MyAttrib1 = attribVal;
}
[ConfigurationProperty("myAttrib1", DefaultValue = "Clowns",
IsRequired = true)]
[StringValidator(InvalidCharacters = "~!@#$%^&*()[]{}/;'\"|\\",
MinLength = 1, MaxLength = 60)]
public String MyAttrib1
{
get
{ return (String)this["myAttrib1"]; }
set
{ this["myAttrib1"] = value; }
}
[ConfigurationProperty("baseUrl")]
public UrlConfigElement BaseUrl
{
get
{ return (UrlConfigElement)this["baseUrl"]; }
set
{ this["baseUrl"] = value; }
}
[ConfigurationProperty("urls")]
public UrlsCollection Urls
{
get
{ return (UrlsCollection)this["urls"]; }
set
{ this["urls"] = value; }
}
[ConfigurationProperty("imgUrls")]
public ImageUrlsCollection ImageUrls
{
get
{ return (ImageUrlsCollection)this["imgUrls"]; }
set
{ this["imgUrls"] = value; }
}
}
public class UrlsCollection : ConfigurationElementCollection
{
public UrlsCollection()
{ }
public override
ConfigurationElementCollectionType CollectionType
{
get
{
return
//ConfigurationElementCollectionType.AddRemoveClearMap;
ConfigurationElementCollectionType.BasicMap;
}
}
protected override
ConfigurationElement CreateNewElement()
{
return new UrlConfigElement();
}
protected override
ConfigurationElement CreateNewElement(
string elementName)
{
return new UrlConfigElement(elementName);
}
protected override Object
GetElementKey(ConfigurationElement element)
{
return ((UrlConfigElement)element).Name;
}
protected override string ElementName
{
get
{
return "url";
}
}
//public new string AddElementName
//{
// get
// { return base.AddElementName; }
// set
// { base.AddElementName = value; }
//}
//public new string ClearElementName
//{
// get
// { return base.ClearElementName; }
// set
// { base.AddElementName = value; }
//}
//public new string RemoveElementName
//{
// get
// { return base.RemoveElementName; }
//}
public new int Count
{
get { return base.Count; }
}
public UrlConfigElement this[int index]
{
get
{
return (UrlConfigElement)BaseGet(index);
}
set
{
if (BaseGet(index) != null)
{
BaseRemoveAt(index);
}
BaseAdd(index, value);
}
}
new public UrlConfigElement this[string Name]
{
get
{
return (UrlConfigElement)BaseGet(Name);
}
}
public int IndexOf(UrlConfigElement url)
{
return BaseIndexOf(url);
}
public void Add(UrlConfigElement url)
{
BaseAdd(url);
// Add custom code here.
}
protected override void
BaseAdd(ConfigurationElement element)
{
BaseAdd(element, false);
// Add custom code here.
}
public void Remove(UrlConfigElement url)
{
if (BaseIndexOf(url) >= 0)
BaseRemove(url.Name);
}
public void RemoveAt(int index)
{
BaseRemoveAt(index);
}
public void Remove(string name)
{
BaseRemove(name);
}
public void Clear()
{
BaseClear();
// Add custom code here.
}
}
public class ImageUrlsCollection : ConfigurationElementCollection
{
public ImageUrlsCollection()
{ }
public override
ConfigurationElementCollectionType CollectionType
{
get
{
return
ConfigurationElementCollectionType.BasicMap;
}
}
protected override
ConfigurationElement CreateNewElement()
{
return new ImageUrlConfigElement();
}
protected override
ConfigurationElement CreateNewElement(
string elementName)
{
return new ImageUrlConfigElement(elementName);
}
protected override Object
GetElementKey(ConfigurationElement element)
{
return ((ImageUrlConfigElement)element).Name;
}
protected override string ElementName
{
get
{
return "imgUrl";
}
}
public ImageUrlConfigElement this[int index]
{
get
{
return (ImageUrlConfigElement)BaseGet(index);
}
set
{
if (BaseGet(index) != null)
{
BaseRemoveAt(index);
}
BaseAdd(index, value);
}
}
new public ImageUrlConfigElement this[string Name]
{
get
{
return (ImageUrlConfigElement)BaseGet(Name);
}
}
public int IndexOf(ImageUrlConfigElement url)
{
return BaseIndexOf(url);
}
public void Add(ImageUrlConfigElement url)
{
BaseAdd(url);
// Add custom code here.
}
protected override void
BaseAdd(ConfigurationElement element)
{
BaseAdd(element, false);
// Add custom code here.
}
public void Remove(ImageUrlConfigElement url)
{
if (BaseIndexOf(url) >= 0)
BaseRemove(url.Name);
}
public void RemoveAt(int index)
{
BaseRemoveAt(index);
}
public void Remove(string name)
{
BaseRemove(name);
}
public void Clear()
{
BaseClear();
// Add custom code here.
}
}
public class UrlConfigElement : ConfigurationElement
{
public UrlConfigElement()
{
}
public UrlConfigElement(string name)
{
Name = name;
}
public UrlConfigElement(string name, string url)
{
Name = name;
Url = url;
}
[ConfigurationProperty("name", IsRequired = true)]
public String Name
{
get
{ return (String)this["name"]; }
set
{ this["name"] = value; }
}
[ConfigurationProperty("url", IsRequired = true)]
public String Url
{
get
{ return (String)this["url"]; }
set
{ this["url"] = value; }
}
[ConfigurationProperty("port")]
public int Port
{
get
{ return (int)this["port"]; }
set
{ this["port"] = value; }
}
}
public class ImageUrlConfigElement : ConfigurationElement
{
public ImageUrlConfigElement()
{
}
public ImageUrlConfigElement(string name)
{
Name = name;
}
public ImageUrlConfigElement(string name, string url)
{
Name = name;
Url = url;
}
[ConfigurationProperty("name", IsRequired = true)]
public String Name
{
get
{ return (String)this["name"]; }
set
{ this["name"] = value; }
}
[ConfigurationProperty("url", IsRequired = true)]
public String Url
{
get
{ return (String)this["url"]; }
set
{ this["url"] = value; }
}
}
}
=====================================
========webconfig file=========
<configuration>
<configSections>
<section name="MyUrls" type="ConfigLib.UrlsSection, ConfigLib"
allowDefinition="Everywhere" allowExeDefinition="MachineToApplication"
restartOnExternalChanges="true"/>
<!-- Other <section> and <sectionGroup> elements. -->
</configSections>
<MyUrls myAttrib1="Clowns">
<urls>
<url name="Microsoft1" url="
http://www.microsoft1.com" port="0"/>
<url name="Microsoft2" url="
http://www.microsoft2.com" port="0"/>
<url name="Microsoft3" url="
http://www.microsoft3.com" port="0"/>
</urls>
<imgUrls>
<imgUrl name="Image1" url="
http://www.microsoft.com/image1.jpg"/>
<imgUrl name="Image2" url="
http://www.microsoft.com/image2.jpg"/>
<imgUrl name="Image3" url="
http://www.microsoft.com/image3.jpg"/>
</imgUrls>
</MyUrls>
===========================
Hope this helps.
Regards,
Steven Cheng
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.