Today a member of the VBcentral.nl community asked a question in one of our news groups. He was wondering if is there is a smart way to store configuration data in a file. I promised him to make a small sample how to use XmlSerialization to store data in a Xml-file. And of course, how to read it back from that file.


The first thing you have to do is create a class and define all the properties you need. For each value you want to store, you define a public property. In my example I have created a class with the name MyConfiguration and I have added two properties: PathExportFiles and PathImportFiles, both of the datatype String. However, if needed you can also use other datatypes like Integer or for example Date.

Public Class MyConfiguration
 
    Public Property PathExportFiles As String
    Public Property PathImportFiles As String
 
End Class

Then comes the part to store the data. To demonstrate this I have created a small Windows Forms application with a couple of controls on it. The most important are the two TextBoxes to enter the configuration data and two buttons. One button for writing the data to a Xml-file and one for reading and showing the data. My form looks like the screenshot below.
configtoxmlserialization
To store the data we need to populate the properties of our MyConfiguration class and use serialization to convert it to Xml. A StreamWriter is used to save the Xml actually to file. The code is straight forward. In the first few lines an instance is created of our MyConfiguration class and the properties are populated with the values entered in the TextBoxes. Then we create a serializer of the type of our MyConfiguration class. We need to tell the serializer which kind of object we will provide, so it knows which properties it will contain. The first argument of the Serialize() method is the stream (writer) to use. We defined that stream with an instance of StreamWriter class. I have used here the Using statement, so I’m sure the file is always closed and the used resources are disposed.

Imports System.IO
Imports System.Xml.Serialization
 
...
 
Private Sub WriteButton_Click() Handles WriteButton.Click
 
  Dim myConfiguration As New MyConfiguration
 
  ' Get the data
  myConfiguration.PathExportFiles = PathExportFilesTextBox.Text
  myConfiguration.PathImportFiles = PathImportFilesTextBox.Text
         
  ' Create the Serializer --> use our MyConfiguration type
  Dim serializer = New XmlSerializer(GetType(MyConfiguration))
 
  ' Create a writer and serialise our data
  Using writer = New StreamWriter("D:\MyConfiguration.xml")
    serializer.Serialize(writer, myConfiguration)
  End Using
 
End Sub

When data is entered in the TextBoxes and is written to file, the following Xml is created. You definitely can recognize the property names and the their values.

<!--?xml version="1.0" encoding="utf-8"?-->
<myconfiguration xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <pathexportfiles>D:\MyExportFiles\</pathexportfiles>
  <pathimportfiles>D:\MyImportFiles\</pathimportfiles>
</myconfiguration>

If you understand how to use serialization to write data to file, reading data is not that hard. It uses the same concept, with the difference that we will now use a StreamReader and call the Deserialize() method on the serializer.

Private Sub ReadButton_Click() Handles ReadButton.Click
         
  Dim myConfiguration As MyConfiguration
 
  ' Create the Serializer --> use our MyConfiguration type 
  Dim serializer = New XmlSerializer(GetType(MyConfiguration))
 
  ' Create a reader and deserialize our data
  Using reader = New StreamReader("D:\MyConfiguration.xml")
    myConfiguration = CType(serializer.Deserialize(reader), MyConfiguration)
  End Using
         
  ' Show the data 
  PathExportFilesTextBox.Text = myConfiguration.PathExportFiles
  PathImportFilesTextBox.Text = myConfiguration.PathImportFiles
         
End Sub

In a production environment you have to add some validation, like validation on the entered data or check if the file exists. But to explain the main concept and to keep it simple I decided to leave that code from my example.