Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read multiple values in C# app.config file?

I want to read the following app.config file.. How to read it? Do I need to change anything in order to read the file ??

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<Users>
  <add username = "Dinesh" password ="Password" domain ="MyCompany" />
 <add username = "Kumar" password ="Password" domain ="MyCompany" />
</Users>
</configuration>
like image 452
Dineshkumar Avatar asked Feb 19 '14 10:02

Dineshkumar


People also ask

Can you return multiple values in C?

In C or C++, we cannot return multiple values from a function directly. In this section, we will see how to use some trick to return more than one value from a function. We can return more than one values from a function by using the method called “call by address”, or “call by reference”.

Can scanf read multiple values?

Inputting Multiple ValuesIf you have multiple format specifiers within the string argument of scanf, you can input multiple values. All you need to do is to separate each format specifier with a DELIMITER - a string that separates variables.

Which command is used to read multiple data values of different data types in C?

Using fscanf and fprintf with stdin and stdout We can also use the fscanf and fprintf function to read data from the standard input stdin like keyboard and write data to the standard output stdout like monitor.


2 Answers

I think you should implement a section.

I made some sample code that might be exactly what you want:

using System.Collections.Generic;
using System.Configuration;
using System.Linq;

namespace ConsoleApplication1
{
    public sealed class UsersConfigMapSection : ConfigurationSection
    {
        private static UsersConfigMapSection config = ConfigurationManager.GetSection("Users") as UsersConfigMapSection;

        public static UsersConfigMapSection Config
        {
            get
            {
                return config;
            }
        }

        [ConfigurationProperty("", IsRequired = true, IsDefaultCollection = true)]
        private UsersConfigMapConfigElements Settings
        {
            get { return (UsersConfigMapConfigElements)this[""]; }
            set { this[""] = value; }
        }

        public IEnumerable<UsersConfigMapConfigElement> SettingsList
        {
            get { return this.Settings.Cast<UsersConfigMapConfigElement>(); }
        }
    }

    public sealed class UsersConfigMapConfigElements : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new UsersConfigMapConfigElement();
        }
        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((UsersConfigMapConfigElement)element).Username;
        }
    }

    public sealed class UsersConfigMapConfigElement : ConfigurationElement
    {
        [ConfigurationProperty("username", IsKey = true, IsRequired = true)]
        public string Username
        {
            get { return (string)base["username"]; }
            set { base["username"] = value; }
        }

        [ConfigurationProperty("password", IsRequired = true)]
        public string Password
        {
            get { return (string)base["password"]; }
            set { base["password"] = value; }
        }

        [ConfigurationProperty("domain", IsRequired = true)]
        public string Domain
        {
            get { return (string)base["domain"]; }
            set { base["domain"] = value; }
        }
    }
}

Then you extract users from your config file like this:

var users = UsersConfigMapSection.Config.SettingsList.ToList();

And finally your config file should look like this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="Users" type="ConsoleApplication1.UsersConfigMapSection, ConsoleApplication1"/>
  </configSections>
  <Users>
    <add username = "Dinesh" password ="Password" domain ="MyCompany" />
    <add username = "Kumar" password ="Password" domain ="MyCompany" />
  </Users>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
</configuration>
like image 161
pepo Avatar answered Sep 20 '22 01:09

pepo


or you can use this work-around to achieve the same ...

<add key="username" value="A,B,C"/>
And

string[] mykey = ConfigurationManager.AppSettings["username"].Split(',');
like image 24
Aftab Ahmed Avatar answered Sep 18 '22 01:09

Aftab Ahmed