Public Class WorkstationSettings ' Internal fields for storing the data Private _SQLServerName As String Private _IP As String Private _ColorScheme As String ' Public Properties for exposing the data Public Property SQLServerName() As String Get Return _SQLServerName End Get Set(ByVal Value As String) _SQLServerName = Value End Set End Property Public Property IP() As String Get Return _IP End Get Set(ByVal Value As String) _IP = Value End Set End Property Public Property ColorScheme() As String Get Return _ColorScheme End Get Set(ByVal Value As String) _ColorScheme = Value End Set End Property End ClassOf special note is that the class only has public properties, not public fields. This is important when defining classes because you can add functionality to the properties at a later date without the need to re-compile the programs that use the class.
' Place the Imports statements at the top of your code file Imports System.Xml.Serialization Imports System.IO ' Create an object to hold the settings based on the WorkstationSettings Class Dim Settings As new WorkstationSettings ' Create an XmlSerializer object to perform the reading and writing of the XML file Dim mySerializer As XmlSerializer = New XmlSerializer(GetType(WorkstationSettings)) ' Read the settings from the XML file: ' Open the XML file Dim myFileStream As FileStream = New FileStream("myFileName.xml", FileMode.Open) ' Read the XML file and copy the contents directly to the object that holds the settings Settings = mySerializer.Deserialize(myFileStream) ' Don't forget to close the file! myFileStream.Close() ' Set the properties of the Settings object: With Settings .SQLServerName = "localhost" .IP = "192.168.1.1" .ColorScheme = "Clouds" End With ' Write the settings to an XML file: ' Create/Overwrite the XML File Dim myWriter As StreamWriter = New StreamWriter("myFileName.xml") ' Write the object contents directly to the XML file mySerializer.Serialize(myWriter, Settings) ' Close the file myWriter.Close()I've left out any error checking for brevity's sake. The code requires some Try..Catch blocks to perform the error trapping because you are reading and writing files.