Click here to Skip to main content
13,251,760 members (33,116 online)
Click here to Skip to main content
Add your own
alternative version


31 bookmarked
Posted 9 Sep 2005

Read/Write Configuration in XML file

, 9 Sep 2005
Rate this:
Please Sign up or sign in to vote.
An article on how to read/write App configuration data in an easy way.

Sample Image


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


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.

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

txtCountry.Text = 

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:


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:

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

Remove element:

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


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


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


About the Author

Michael Chao
Canada Canada
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
Member 78911884-May-11 2:40
memberMember 78911884-May-11 2:40 
GeneralStill very usefull Pin
Piet Pelle7-Jun-09 5:55
memberPiet Pelle7-Jun-09 5:55 
GeneralGetting all Keys/Values for a config section Pin
Heinz Ruebe31-Jul-08 6:22
memberHeinz Ruebe31-Jul-08 6:22 
Generalmy questions (mabey nothing to do about this article) Pin
cooleader30-Jul-07 0:19
membercooleader30-Jul-07 0:19 
GeneralEasier Use Pin
feiry24-Jan-07 16:36
memberfeiry24-Jan-07 16:36 
GeneralJust what I was looking for. Pin
Davepow162-Dec-05 20:30
memberDavepow162-Dec-05 20:30 
Questionhow about serialization? Pin
Chris_O11-Sep-05 6:52
memberChris_O11-Sep-05 6:52 

try this:

Create any config object you like.
Then load and save that object with serialization.
If you have complex objects choose Soap.
For example: (very quick and dirty, but I think it shows how serialization can work...)

Imports System.Runtime.Serialization.Formatters.Soap
Imports System.IO

public Class MyConfig

private m_Value1 as string
private m_Value2 as integer

Public Property Value1 () As string
Return m_Value1
End Get
Set(ByVal Value As string)
m_Value1 = Value
End Set
End Property

Public Property Value2 () As integer
Return m_Value2
End Get
Set(ByVal Value As integer)
m_Value2 = Value
End Set
End Property

end class

public class MyApplication

private Sub ConfigSerialization()

Dim Path As String
Path = Application.StartupPath
Dim oMySoap As New MySoapSerializer
dim oMyConfig as new MyConfig
oMyConfig.Value1 = "test"
oMyConfig.Value1 = 123456
oMySoap.Serialize(oMyConfig, "Config", Path)
Catch ex As ApplicationException
'todo: Ex Handling
End Try

End Sub

Private Sub LoadConfig()

Dim MySoap As New MySoapSerializer
dim oMyConfig as MyConfig
Dim stemp As String
stemp = Application.StartupPath + "Config.xml"
oMyConfig = MySoap.Deserialize(stemp)

End Sub

end class

Public Class MySoapSerializer

Public Sub New()
End Sub

Public Sub Serialize(ByVal Obj As Object, ByVal Name As String)

Dim S As Stream = File.Create(Name + ".xml")

Dim SF As SoapFormatter = New SoapFormatter
SF.Serialize(S, Obj)

End Sub

Public Function Deserialize(ByVal Name_Path As String) As Object

Dim file As FileStream = New FileStream(Name_Path, FileMode.Open)
Dim bf As SoapFormatter = New SoapFormatter
Dim obj As Object = bf.Deserialize(file)

Return obj

End Function

End Class

AnswerThanks for your feedback, it's so interesting Pin
michael zhao11-Sep-05 7:47
membermichael zhao11-Sep-05 7: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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171114.1 | Last Updated 9 Sep 2005
Article Copyright 2005 by Michael Chao
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid