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>
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”.
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.
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.
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>
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(',');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With