Nim
Nim

Reputation: 376

Retrieve multiple keys from custom config c#

My app config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="Database" type="Database_Server_Settings.Database_Server_SettingsConfigSection, BMS Internal Database Restore" />
  </configSections>
  <Database>
    <Databases>
      <add id="BMSCarlyle" Server_Name="bmslondon01" Database_Name="Everest_Carlyle" Database_Backup_Location="D:\SQL Backups\Carlyle\Everest_Carlyle.bak" TradeDesk_Location="C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\Carlyle.mdf" TradeDesk_Log_Location="C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\Carlyle_log.ldf" />
      <add id="bmsnim" Server_Name="bmslondon01" Database_Name="Everst_Nim" Database_Backup_Location="D:\SQL Backups\Carlyle\Everest_Carlyle.bak" TradeDesk_Location="C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\Carlyle.mdf" TradeDesk_Log_Location="C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\Carlyle_log.ldf" />
    </Databases>
  </Database>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
  </startup>
</configuration>

Now when I try and retrieve it like this

DBID.Items.Add(section.DatabaseSettings["BMSCarlyle"].id)

I retrieve the ID absolutely fine.

However, I want to add all the Ids not just BMSCarlyle to my list therefore

DBID.Items.Add(section.DatabaseSettings[].id)

However, this doesn't appear to work. Can anyone please suggest what I need to put between the "[]"?

Dave here is the handler code

namespace Database_Server_Settings
{
    public class Database_Server_SettingsConfigSection : ConfigurationSection
    {
        [ConfigurationProperty("Databases")]
        public DatabaseCollection DatabaseSettings
        {
            get
            {
                return ((DatabaseCollection)(base["Databases"]));
            }
        }
    }

    [ConfigurationCollection( typeof (DatabaseElement))]
    public class DatabaseCollection : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new DatabaseElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((DatabaseElement)(element)).id;
        }

        public DatabaseElement this [string idx]
        {
            get
            {
                return (DatabaseElement)BaseGet(idx);
            }
        }
    }

    public class DatabaseElement : ConfigurationElement
    {
        [ConfigurationProperty("id", DefaultValue="", IsKey=true, IsRequired=true)]
        public string id
        {
            get
            {
                return ((string)(base["id"]));
            }
            set
            {
                base["id"] = value;
            }
        }
        [ConfigurationProperty("Server_Name", DefaultValue="", IsKey=false, IsRequired=false)]
        public string Server_Name
        {
            get
            {
                return ((string)(base["Server_Name"]));
            }
            set
            {
                base["Server_Name"] = value;
            }
        }
        [ConfigurationProperty("Database_Name", DefaultValue = "", IsKey = false, IsRequired = false)]
        public string Database_Name
        {
            get
            {
                return ((string)(base["Database_Name"]));
            }
            set
            {
                base["Database_Name"] = value;
            }
        }
        [ConfigurationProperty("Database_Backup_Location", DefaultValue = "", IsKey = false, IsRequired = false)]
        public string Database_Backup_Location
        {
            get
            {
                return ((string)(base["Database_Backup_Location"]));
            }
            set
            {
                base["Database_Backup_Location"] = value;
            }
        }
        [ConfigurationProperty("TradeDesk_Location", DefaultValue = "", IsKey = false, IsRequired = false)]
        public string TradeDesk_Location
        {
            get
            {
                return ((string)(base["TradeDesk_Location"]));
            }
            set
            {
                base["TradeDesk_Location"] = value;
            }
        }
        [ConfigurationProperty("TradeDesk_Log_Location", DefaultValue = "", IsKey = false, IsRequired = false)]
        public string TradeDesk_Log_Location
        {
            get
            {
                return ((string)(base["TradeDesk_Log_Location"]));
            }
            set
            {
                base["TradeDesk_Log_Location"] = value;
            }
        }
    }
}

Upvotes: 0

Views: 599

Answers (1)

Sanjay Sahani
Sanjay Sahani

Reputation: 580

See if you have DBID.Items.AddRange method then first example otherwise second option-

DBID.Items.AddRange(section.DatabaseSettings.Select(p=>p.id).ToList());

or

foreach(var item in section.DatabaseSettings.Select(p=>p.id))
{
DBID.Items.Add(item);
}



[ConfigurationCollection( typeof (DatabaseElement))]
    public class DatabaseCollection : ConfigurationElementCollection, IEnumerable<DatabaseElement>
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new DatabaseElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((DatabaseElement)(element)).id;
        }

        public DatabaseElement this [string idx]
        {
            get
            {
                return (DatabaseElement)BaseGet(idx);
            }
        }

    #region IEnumerable<DatabaseElement> Members


        public new IEnumerator<DatabaseElement> GetEnumerator()
        {
            int count = base.Count;
            for (int i = 0; i < count; i++)
            {
                yield return base.BaseGet(i) as DatabaseElement;
            }
        }

        #endregion
    }

Upvotes: 1

Related Questions