Using an XML file for application configuration

Letting the end user have control over critical elements of your vb.net application.

One of the most common questions I see involves developers trying to allow the end user to set program options, or control the location of critical application files. This blog is an attempt to assist with this endeavor!!

1) Create a Windows project under any name you wish, and then add an xml file by right clicking on the project name, and select "Add a new item" and in the dialog box select an XML file.

2) Type the following into your xml file using what you want for your mail server name. Any future settings you wish to add can be placed by typing another Key Name in just the same manner below the first one called "mailserver".

<?xml version="1.0" encoding="utf-8"?>
<Section Name="Settings">
<Key Name="mailserver" Value="mail.charter.net" />
</Section>

3) In the Form1 graciously created automatically for you by Visual Studio designer add a textbox, a label with its .Text property set to "SMTP Mail Server" and a button with the .Text property set to "Save".

4) Switch to code view by double clicking on the form. Add the following under the "Windows Form Designer Generated Code":

5) Under the form load event type what is in white. The black are comments to help you understand what is happening.

Private Sub ReadXmlConfig()

Create Xml Document instance
Dim xmlDoc As XmlDocument = New XmlDocument

load the file.
xmlDoc.Load(Application.StartupPath & "\settings.xml")

Create an array of key nodes of your xml file. Key nodes are the various elements of the xml file that hold your settings.
Dim keyNodeList As XmlNodeList = xmlDoc.DocumentElement.SelectNodes("Key")

create an instance of a single keynode which we need to find the single key we are looking for
Dim keyNode As XmlNode

Now we loop through the nodes looking for the keynode holding our mail server value.
For Each keyNode In keyNodeList

‘Each keynode has attributes that together form a "collection." similar to an array. In this case we are seeking the "name" of the node as its attribute..
Dim attribs As XmlAttributeCollection = keyNode.Attributes

Like we did with the keynodes we create a an instance of a single "attribute" called "Name" in which we can look for our value.
Dim attrib As XmlAttribute = attribs("Name")

We are stating that in this loop if the mail server value is found we want to take the attribute of this keynode which is the mailserver value entered earlier and assign it to the string called "mailserver". Each keynode has attributes that together form a "collection." In this case we are seeking the "name" of the node as its ‘attribute..
If attrib.Value = "mailserver" Then
mailserversetting = attribs("Value").Value.ToString()
End If
Next

End Sub

6) In the form load event type

ReadXmlConfig
TextBox1.Text = mailserver

7) Build and run your project. You should see your mail server setting you assigned appear in the textbox on form load. After admiring your work, shut down the program.

8) Now we will change the value of this key as if the end user wanted to change the name of their mail server. Under the ReadXmlConfig sub place another type the following:

Sub EnterUserSettings()

Dim strOldEmailValue As String
Try

Create Xml Document instance
Dim xmlDoc As XmlDocument = New XmlDocument
load the file.
xmlDoc.Load(Application.StartupPath & "\settings.xml")

Create an array of key nodes of your xml file. Key nodes are the various elements of the xml file that hold your settings.
Dim keyNodeList As XmlNodeList = xmlDoc.DocumentElement.SelectNodes("Key")

create an instance of a single keynode which we need to find the single key we are looking for
Dim keyNode As XmlNode

Now we loop through the nodes looking for the keynode holding our mail server value.
For Each keyNode In keyNodeList
Each keynode has attributes that together form a "collection." similar to an array. In this case we are seeking the "name" of the node as its attribute..
Dim attribs As XmlAttributeCollection = keyNode.Attributes

Like we did with the keynodes we create a an instance of a single "attribute" called "Name" in which we can look for our value.
Dim attrib As XmlAttribute = attribs("Name")

We are stating that in this loop if the mail server value is found we want to take the attribute of this keynode which is the mailserver value entered earlier and assign it to the string called "mailserver". Each keynode has attributes that together form a "collection." In this case we are seeking the "name" of the node as its ‘attribute..
If attrib.Value = mailserver Then

assigning the old value to a variable – in case the settings change fails
strOldValue = attribs("Value").Value.ToString()

set the new value of this key now
attribs("Value").Value = TextBox1.Text
End If

save the xml file
xmlDoc.Save(Application.StartupPath & "\settings.xml")
Next

letting the user know of the change
MsgBox("Your mail server is " & chooselocation, MsgBoxStyle.Information)
Exit Sub
End If
Catch As System.IO.IOException

if saving the file failed

Select Case MessageBox.Show("There was a problem Saving the file, would you like to try another location?")
Case DialogResult.Ok
Dim od as New OpenFileDialog
od.ShowDialog
Dim FileLocation as String = od.FullName
xmlDoc.Save("Blah Blah Blah)
Case DialogResult.No
MessageBox.Show(ex.Message, "File Not Saved")
‘ or whatever routine you want.
End Select
MsgBox("SETTING " & mailserversetting.ToUpper & " FAILED" & vbCrLf & ex.ToString)

End Try

9) In the designer double click on the button1 and add the sub "EnterUserSettings". Compile and run your project and change the value of the email server in TextBox1.

That’s all folks!! That wasn’t so bad was it? Please be sure to leave comments if it helped you and where you were referred. Thanks.

Advertisements
  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: