Click here to Skip to main content
15,883,883 members
Articles / Programming Languages / XML
Article

Read/Write Configuration in XML file

Rate me:
Please Sign up or sign in to vote.
3.52/5 (20 votes)
9 Sep 20051 min read 88.9K   2.1K   31   8
An article on how to read/write App configuration data in an easy way.

Sample Image

Introduction

A class with a simple, easy to use interface, is ready to help every one to read/write application configuration in an effective way.

Background

First of all, I don't like ConfigurationSettings provided by the .NET framework, as I can not change the config as I want in the App. I did see many articles on the topic, but either they are too big and complex, get me distracted, or can not meet my requirement. My goal is simple: provide a default setting when first launching the App, and on exit, "remember" the user's preference. Registry is another option, but I prefer XML files, which you can open and check more conveniently.

Using the code

All the settings is set by key/value pairs in an XML file, you can insert/update/delete any key/value. The three major public methods are: GetValue, SetValue, removeElement, it's that simple. By default, the XML file should be put in the same folder as your EXE file.

XML
GetValue, key= "CountryLoc"
C#
Config config = new Config();
config.cfgFile = "app.config"; 

txtCountry.Text = 
  config.GetValue("//appSettings//add[@key='CountryLoc']");

I exposed //appSettings in the interface, as you may need access to key/value in different sections. For instance, if you GetValue from key1 in Company section, you can use:

C#
config.GetValue("//Company//add[@key='key1']");

If the section is new, you need to set it up, for example, manually add <Company> </Company> into <configuration>...</configuration>. As in my case, I only have one section, so if you do not like manual thing, you are very welcome to put your code to enhance it. SetValue is quite similar:

C#
config.SetValue("//appSettings//add[@key='" 
                   + txtKey.Text + "']", txtValue.Text);
// config.SetValue("//Company//add[@key='" + 
                     txtKey.Text + "']", txtValue.Text);

Remove element:

C#
//config.removeElement("//Company//add[@key='" + 
//                         txtKey2.Text + "']");
config.removeElement("//appSettings//add[@key='" + 
                             txtKey2.Text + "']");

Reference

This article is based on some code I found on the internet which I have forgotten where it is.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Member 78911884-May-11 1:40
Member 78911884-May-11 1:40 
GeneralStill very usefull Pin
Piet Pelle7-Jun-09 4:55
Piet Pelle7-Jun-09 4:55 
GeneralGetting all Keys/Values for a config section Pin
Heinz Ruebe31-Jul-08 5:22
Heinz Ruebe31-Jul-08 5:22 
Generalmy questions (mabey nothing to do about this article) Pin
cooleader29-Jul-07 23:19
cooleader29-Jul-07 23:19 
GeneralEasier Use Pin
feiry24-Jan-07 15:36
feiry24-Jan-07 15:36 
GeneralJust what I was looking for. Pin
Davepow162-Dec-05 19:30
Davepow162-Dec-05 19:30 
Very easy to use. However to make the xml class more independent I removed the Application.path and used that in my form instead of from the class. I made some other visual adjustments to meet my likings and it works great. Thanks for the info.

www.cocoontech.com
Questionhow about serialization? Pin
Chris_O11-Sep-05 5:52
Chris_O11-Sep-05 5:52 
AnswerThanks for your feedback, it's so interesting Pin
Michael Chao11-Sep-05 6:47
Michael Chao11-Sep-05 6:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.