Note that there are some explanatory texts on larger screens.

plurals
  1. POCustom config section containing collection
    primarykey
    data
    text
    <p>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.</p> <p>The error I get when I run the code in a console app is ' <em>Unrecognized attribute 'extension'. Note that attribute names are case-sensitive.</em>'</p> <p>The code in the main app to get things going is</p> <pre><code>var conf = ConfigurationManager.GetSection("uploadDirector"); </code></pre> <p>and this is where the exception appears.</p> <p>This is the config section I am hoping/trying to achieve</p> <pre><code>&lt;AuthorisedClients&gt; &lt;AuthorisedClient name="Client"&gt; &lt;Queue id="1" /&gt; &lt;Queue id="7" /&gt; &lt;/AuthorisedClient&gt; &lt;AuthorisedClient name="Client2"&gt; &lt;Queue id="3" /&gt; &lt;Queue id="4" /&gt; &lt;/AuthorisedClient&gt; &lt;/AuthorisedClients&gt; </code></pre> <p>Here's the code I have got from the web</p> <p><em>.config file</em></p> <pre><code>&lt;uploadDirector&gt; &lt;filegroup name="documents" defaultDirectory="/documents/"&gt; &lt;clear/&gt; &lt;add extension="pdf" mime="application/pdf" maxsize="100"/&gt; &lt;add extension="doc" mime="application/word" maxsize="500"/&gt; &lt;/filegroup&gt; &lt;filegroup name="images"&gt; &lt;clear/&gt; &lt;add extension="gif" mime="image/gif" maxsize="100"/&gt; &lt;/filegroup&gt; &lt;/uploadDirector&gt; </code></pre> <p><em>UploadDirectorConfigSection.cs</em></p> <pre><code>public class UploadDirectorConfigSection : ConfigurationSection { private string _rootPath; public UploadDirectorConfigSection() { } [ConfigurationProperty("rootpath", DefaultValue="/", IsRequired=false, IsKey=false)] [StringValidator(InvalidCharacters=@"~!.@#$%^&amp;*()[]{};'\|\\")] 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; } } } </code></pre> <p><em>FileGroupCollection.cs</em></p> <pre><code>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); } } } </code></pre> <p><em>FileGroupElement.cs</em></p> <pre><code>public class FileGroupElement : ConfigurationElement { [ConfigurationProperty("name", IsKey=true, IsRequired = true)] [StringValidator(InvalidCharacters = @" ~.!@#$%^&amp;*()[]{}/;'""|\")] 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[""]; } } } </code></pre> <p><em>FileInfoCollection.cs</em></p> <pre><code>public class FileInfoCollection : ConfigurationElementCollection { protected override ConfigurationElement CreateNewElement() { return new FileInfoCollection(); } protected override object GetElementKey(ConfigurationElement element) { return ((FileInfoElement) element).Extension; } } </code></pre> <p><em>FileInfoElement.cs</em></p> <pre><code>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; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload