.net - 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 to read the file ??

  & lt ;? Xml version = "1.0" encoding = "UTF-8"? & Gt; & Lt; Configuration & gt; & Lt; Users & gt; & Lt; Username = "dinesh" password = "password" domain = "micropany" /> & Lt; Username = "Kumar" password = "password" domain = "micropany" /> & Lt; / User & gt; & Lt; / Configuration & gt;  

I think you should apply a section

I have created some sample code that exactly what you want:

  using System.Collections.Generic; Using System.configuration; Using System.Linq; Namespace Console Application 1 {Public Seal Class User Configure MX Special: Configuration Description {Personal Static User Config Info Details Configuration = Configuration Manager. UsersConfigMapSection; as Gatesignation ("Users"); Public Static User ConfigMax Signal Configuration {get {return config; }} [Configuration property ("", IsRequired = true, IsDefaultCollection = true)] Personal User Configure Map Config Elementes Settings {Get {Return (User Confidentialmap ConfigAlments) This [""]; } Set {this ["" "] = value; }} Public IEnumerable & lt; UsersConfigMapConfigElement & gt; Setting list {return it back. Settings. Cast & lt; UserContact MapConfigameName & gt; (); }}} Public Sealed User UsersConfigMapConfigElements: ConfigurationElementCollection {Safe Override Configuration Element CreateNewElement () {Return New UsersConfigMapConfigElement}; } Safe Override Object GetElementKey (Configuration Element Elements) {Return ((UsersConfigMapConfigElement) element). User name; Public Seal Class User Config GIF Confidential Element: Configuration Element {{ConfigurationProperty ("User Name", IsKey = true, IsRequired = true)) Public String User Name Get {{Return (String) Base ["User Name"]; } Set {base ["username"] = value; }} [Configuration property ("password", esurpyire = true)] public string password {received {return (string) base ["password"]; } Set {base ["password"] = value; }} [Configuration property ("domain", IsRequired = true)] Get public string domain {{return (string) base ["domain"]; } Set {base ["domain"] = value; }}}}  

Then you can remove the user from your config file like this:

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

And at the end your config files should look like this:

  & lt ;? Xml version = "1.0" encoding = "UTF-8"? & Gt; & Lt; Configuration & gt; & Lt; ConfigSections & gt; & Lt; Section name = "user" type = "console application 1.supercode map special, console application1" /> & Lt; / ConfigSections & gt; & Lt; Users & gt; & Lt; Username = "dinesh" password = "password" domain = "micropany" /> & Lt; Username = "Kumar" password = "password" domain = "micropany" /> & Lt; / User & gt; & Lt; Startup & gt; & Lt; Supported serial version = "v4.0" sku = ".NETFramework, version = v4.5" /> & Lt; / Startup & gt; & Lt; / Configuration & gt;  

Comments