Getting error while reading nested custom section from config file in c#


i gettign error while reading custom config section.

i have nested custom config section defined below-

<?xml version="1.0" encoding="utf-8" ?>

<configuration>
  <configsections>
    <section name="personalinfodata" type="personalcustomsectiondemo.confighelper,personalcustomsectiondemo"/>
  </configsections>
  <personalinfodata>
  <personalinfo>
    <add key="name" value="xyz" />
    <address>
      <add key="address line 1" value=" " />
      <add key="address line 2" value="" />
      <add key="city" value=" " />
      <add key="pin" value=" " />
    </address>
    <add key="age" value=" " />
    <add key="gender" value=" " />
  </personalinfo>
  </personalinfodata>

</configuration>

i have written confighelper class follows-

                               

using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.threading.tasks;
using system.configuration;

namespace personalcustomsectiondemo
{

    public class confighelper : configurationsection
    {
      [configurationproperty("personalinfo")]
        public personalinfocollection infokeys
        {
            { return ((personalinfocollection)(base["personalinfo"])); }
        }


    }

   [configurationcollection(typeof(personalinfoelement))]
    public class personalinfocollection : configurationelementcollection
    {
        protected override configurationelement createnewelement()
        {
            return new personalinfoelement();
        }

        protected override object getelementkey(configurationelement element)
        {
            return ((personalinfoelement)(element)).key;
        }

        public personalinfoelement this[int idx]
        {
            get
            {
                return (personalinfoelement)baseget(idx);
            }
        }
    }


  public class personalinfoelement : configurationelement
    {
        [configurationproperty("key", defaultvalue = "",
         iskey = true, isrequired = true)]
        public string key
        {
            get
            {
                return ((string)(base["key"]));
            }
            set
            {
                base["key"] = value;
            }
        }

        [configurationproperty("value",
          defaultvalue = "", iskey = false, isrequired = false)]
        public string value
        {
            get
            {
                return ((string)(base["value"]));
            }
            set
            {
                base["value"] = value;
            }
        }

        //[configurationproperty("address", defaultvalue = "", iskey = false, isrequired = false)]
        //public addresselement addresselement
        //{

        //}



    }

    public class addresselement : configurationelement
    {
        [configurationproperty("key", defaultvalue = "",
         iskey = true, isrequired = true)]
        public string key
        {
            get
            {
                return ((string)(base["key"]));
            }
            set
            {
                base["key"] = value;
            }
        }

        [configurationproperty("value",
          defaultvalue = "", iskey = false, isrequired = false)]
        public string value
        {
            get
            {
                return ((string)(base["value"]));
            }
            set
            {
                base["value"] = value;
            }
        }

        [configurationproperty("address",
          defaultvalue = "", iskey = false, isrequired = false)]
        public string address
        {
            get
            {
                return ((string)(base["address"]));
            }
            set
            {
                base["address"] = value;
            }
        }
    }



}

and trying read section below-

                            

using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.threading.tasks;
using system.configuration;

namespace personalcustomsectiondemo
{
    class program
    {
        static void main(string[] args)
        {

            confighelper section = (confighelper)configurationmanager.getsection("personalinfodata");

            if (section != null)
            {
                foreach (personalinfoelement element in section.infokeys)
                {

                    console.writeline(string.format("key {0}, value {1} ", element.key, element.value));


                }


                console.writeline("\npress key exit...");
                console.readline();
            }
        }
    }
}

please suggest change have do.i tried several solutions nothing working me.please suggest change have do.

you cannot mix configurationelementcollection stuff custom element stuff.  in personalinfo element you're trying treat dictionary key-value pairs you're defining address element right in middle of it. isn't going work. cec assumes add, remove, clear (depending upon collection type , custom naming might configure). if wanted structure specified you'd have create configurationelement included property of cec , set default xml structure doesn't seem add me wouldn't recommend going route.

if xml structure predefined you're going have write lot more code work out. going limiting far future changes go.  if possible recommend adjust xml structure. since appears you're trying collect personal information i'd recommend separating out elements rather using simple dictionary.  example might consider doing this.

<personalinfodata>    <personalinfo age="35" gender="male">       <name firstname="bob" lastname="miller" fullname="bob miller" />       <address isprimary="true">        <line1>123 main st</line1>        <line2>this optional , can excluded</line2>        <city>somewhere</city>        <pin>12345</pin>      </address>    </personalinfo>    </personalinfodata> 

in general use attributes represent single value doesn't need store complex text (line address line) , use elements more complex data or when there can more 1 (like address).  can start attributes , move elements in later versions relatively painlessly.

this becomes easy code each xml element becomes configurationelement , each attribute becomes property on element.  child elements child properties of custom configurationelement type.

please evaluate xml , whether can change , post "updated" xml , we'll try provide corresponding configuration.  note not possible xml structures can used in section things won't work. i'll recommend blog article wrote several years ago reference.

michael taylor
http://blogs.msmvps.com/p3net



Visual Studio Languages  ,  .NET Framework  >  Visual C#



Comments

Popular posts from this blog

Azure DocumentDB Owner resource does not exist

job syspolicy_purge_history job fail in sqlserver 2008

Trying to register with public marketplace error with 'Get-AzureStackStampInformation'