c# - Weird error while accessing an element in a ConfigurationElementCollection -


i have configurationsection configurationelementcollection below:

    <myconfiguration hosturl="https://example.com/ewe/rets">         <sampleconfig username="xzxzxzxzxzx" password="00000000" listname="mylist">           <metadata>             <add name="foldername" value="testfolder"/>             <add name="filename" value="testfilename" />             <add name="title" value="testtitle" />             <add name="description" value="testdescription" />           </metadata>         </sampleconfig>         <whateverconfig username="xzxzxzxzxzx" password="00000000" listname="mylist">           <metadata>             <add name="sitename" value="testfolder"/>             <add name="filename" value="testfilename" />             <add name="title" value="testtitle" />             <add name="description" value="testdescription" />           </metadata>         </whateverconfig> </myconfiguration> 

and code looks this:

    public sealed class myconfiguration: configurationsection     {             [configurationproperty("hosturl", isrequired = true)]             public string hosturl             {                 { return (string)base["hosturl"]; }                 set { base["hosturl"] = value; }             }              [configurationproperty("sampleconfig", isrequired = true)]             public apiconfig sampleconfig             {                 { return (apiconfig)base["sampleconfig"]; }                 set { base["sampleconfig"] = value; }             }              [configurationproperty("whateverconfig", isrequired = true)]             public apiconfig whateverconfig             {                 { return (apiconfig)base["whateverconfig"]; }                 set { base["whateverconfig"] = value; }             }     }          public class apiconfig : configurationelement         {             [configurationproperty("username", isrequired = true)]             public string username             {                 { return (string)base["username"]; }                 set { base["username"] = value; }             }              [configurationproperty("password", isrequired = true)]             public string password             {                 { return (string)base["password"]; }                 set { base["password"] = value; }             }              [configurationproperty("listname", isrequired = true)]             public string listname             {                 { return (string)base["listname"]; }                 set { base["listname"] = value; }             }                      [configurationproperty("metadata", isdefaultcollection = false)]             [configurationcollection(typeof(metadatacollection),                 additemname = "add",                 clearitemsname = "clear",                 removeitemname = "remove")]             public metadatacollection metadata             {                                 {                     return (metadatacollection)base["metadata"];                 }             }     }  public class metadatacollection : configurationelementcollection     {         public metadataconfig this[string key]         {             { return (metadataconfig)baseget(key); }             set             {                 if (baseget(key) != null)                 {                     baseremove(key);                 }                 baseadd(value);             }         }          public void add(metadataconfig metadataconfig)         {             baseadd(metadataconfig);         }          public void clear()         {             baseclear();         }          protected override configurationelement createnewelement()         {             return new metadataconfig();         }          protected override object getelementkey(configurationelement element)         {             return ((metadataconfig)element).sharepointname;         }          public void remove(metadataconfig element)         {             baseremove(element.sharepointname);         }          public void removeat(int index)         {             baseremoveat(index);         }          public void remove(string name)         {             baseremove(name);         }      }  public class metadataconfig : configurationelement     {         [configurationproperty("name", isrequired = true, iskey = true)]         public string name         {             { return (string)base["name"]; }             set { base["name"] = value; }         }          [configurationproperty("value", isrequired = true, iskey = false)]         public string value         {             { return (string)base["value"]; }             set { base["value"] = value; }         } } 

but when try access "description" metadata, nullreferenceexception. because config.sampleconfig.metadata["description"] null. in debug window, can see "metadata" has 4 elements , "description" there.

this true while accessing "title". "foldername" , "filename" fine!!!

i not sure whats wrong , banging head @ last 4 hours!

please me out here.

thanks.

changing getelementkey to:

protected override object getelementkey(configurationelement element) {     return ((metadataconfig)element).name; } 

allows following return: testtitle

console.writeline(serviceconfigsection.sampleconfig.metadata["title"].value); 

Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -