6

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>

3 Answers 3

12

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>
Sign up to request clarification or add additional context in comments.

Comments

1

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(',');

Comments

0

First you need to put your value under <appSettings> then add key to your desired field like this

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
  <add key="username" value="Dinesh" />
<appSettings>
</configuration>

Then You'll need to add a reference to System.Configuration in your references folder.

Now Read your Value ...

String Version = ConfigurationManager.AppSettings["username"];

3 Comments

In case two user names, how will I go ahead ?
then you have to add another key for second user-name.
no if you want to get another username from web.config you need to add another xml tag with different key e.g <add key="username" value="Dinesh" /> and <add key="username1" value="Kumar" /> because in code you will be accessing through keys not by value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.