.net 2.0 Custom Configuration Sections
-
Dear All, I am trying to create a class to handle my custom config sections in my app.config file, but no matter what i tried, i always get this kind of error: Unrecognized attribute 'name'. Note that attribute names are case-sensitive. (D:\Users\xxx\Documents\Visual Studio 2005\Projects\ConsoleApplication2\ConsoleApplication2\bin\Debug\ConsoleApplication2.vshost.exe.config line 10) I had spent about half days to try to fix this annoy error but without any luck. can someone in here shed me a light will be greatly appreciated. This is my app.config file: and this is my source code: using System; using System.Collections.Generic; using System.Text; using System.Configuration; namespace ConsoleApplication2 { public class Class1: ConfigurationSection { [ConfigurationProperty("package")] public PackageVersionCollection PackageVersionItems { get { return ((PackageVersionCollection)(base["package"])); } } } [ConfigurationCollection(typeof(PackageVersionElement))] public class PackageVersionCollection : ConfigurationElementCollection { protected override ConfigurationElement CreateNewElement() { return new PackageVersionElement(); } protected override object GetElementKey(ConfigurationElement element) { return ((PackageVersionElement)(element)).Name; } public PackageVersionElement this[int idx] { get { return (PackageVersionElement)BaseGet(idx); } } } public class PackageVersionElement : ConfigurationElement { [ConfigurationProperty("name", DefaultValue = "", IsKey = true, IsRequired = true)] pu
-
Dear All, I am trying to create a class to handle my custom config sections in my app.config file, but no matter what i tried, i always get this kind of error: Unrecognized attribute 'name'. Note that attribute names are case-sensitive. (D:\Users\xxx\Documents\Visual Studio 2005\Projects\ConsoleApplication2\ConsoleApplication2\bin\Debug\ConsoleApplication2.vshost.exe.config line 10) I had spent about half days to try to fix this annoy error but without any luck. can someone in here shed me a light will be greatly appreciated. This is my app.config file: and this is my source code: using System; using System.Collections.Generic; using System.Text; using System.Configuration; namespace ConsoleApplication2 { public class Class1: ConfigurationSection { [ConfigurationProperty("package")] public PackageVersionCollection PackageVersionItems { get { return ((PackageVersionCollection)(base["package"])); } } } [ConfigurationCollection(typeof(PackageVersionElement))] public class PackageVersionCollection : ConfigurationElementCollection { protected override ConfigurationElement CreateNewElement() { return new PackageVersionElement(); } protected override object GetElementKey(ConfigurationElement element) { return ((PackageVersionElement)(element)).Name; } public PackageVersionElement this[int idx] { get { return (PackageVersionElement)BaseGet(idx); } } } public class PackageVersionElement : ConfigurationElement { [ConfigurationProperty("name", DefaultValue = "", IsKey = true, IsRequired = true)] pu
It is hard to read your code, can you tag it up please. AFAICT, This is what defines the element package
[ConfigurationProperty("package")]
public PackageVersionCollection PackageVersionItems
{
get
{
return ((PackageVersionCollection)(base["package"]));
}
}It should contain a sub-element containing a list of
PackageVersionItems
. But your config does something different:<packageVersionSection>
<packageVersion>
<package name="Web Application" version="1.1.2.1" location="C:\Program Files\TeleMedCare\TMSWebApps" />....The package is an element in the collection. You also define a "name" property here this isn't in the class I've copied. As a rule of thumb, you should make sure your class names always tally up with the property names you intend them for. [edit] fixed the XML markup so it would actually display, haven't done that in a while :-O
-
It is hard to read your code, can you tag it up please. AFAICT, This is what defines the element package
[ConfigurationProperty("package")]
public PackageVersionCollection PackageVersionItems
{
get
{
return ((PackageVersionCollection)(base["package"]));
}
}It should contain a sub-element containing a list of
PackageVersionItems
. But your config does something different:<packageVersionSection>
<packageVersion>
<package name="Web Application" version="1.1.2.1" location="C:\Program Files\TeleMedCare\TMSWebApps" />....The package is an element in the collection. You also define a "name" property here this isn't in the class I've copied. As a rule of thumb, you should make sure your class names always tally up with the property names you intend them for. [edit] fixed the XML markup so it would actually display, haven't done that in a while :-O
It is the issue with my bloody xml file, it should be like this:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="packageVersionSection">
<section name="packageVersion" type="ConsoleApplication2.Class1, ConsoleApplication2" />
</sectionGroup>
</configSections>
<packageVersionSection>
<packageVersion>
<packages>
<add name="Web Application" version="1.1.2.1" location="C:\Program Files\TeleMedCare\TMSWebApps" />
<add name="RPU Applications" version="1.1.2.1" location="C:\Program Files\TeleMedCare\TMSWinApps" />
<add name="Shared Applications" version="1.1.2.0" location="C:\Program Files\TeleMedCare\SharedApp" />
</packages>
</packageVersion>
</packageVersionSection>
</configuration>I just missed the 'packages' element in my previous posting and i should use 'add'. Thanks a lot for ur help anyway.
-
It is the issue with my bloody xml file, it should be like this:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="packageVersionSection">
<section name="packageVersion" type="ConsoleApplication2.Class1, ConsoleApplication2" />
</sectionGroup>
</configSections>
<packageVersionSection>
<packageVersion>
<packages>
<add name="Web Application" version="1.1.2.1" location="C:\Program Files\TeleMedCare\TMSWebApps" />
<add name="RPU Applications" version="1.1.2.1" location="C:\Program Files\TeleMedCare\TMSWinApps" />
<add name="Shared Applications" version="1.1.2.0" location="C:\Program Files\TeleMedCare\SharedApp" />
</packages>
</packageVersion>
</packageVersionSection>
</configuration>I just missed the 'packages' element in my previous posting and i should use 'add'. Thanks a lot for ur help anyway.
Easily done, at least it's fixed!