logo
Welcome Guest! To enable all features please Login. New Registrations are disabled.

Notification

Icon
Error

Options
Go to last post Go to first unread
Sjarl  
#1 Posted : Saturday, April 10, 2010 9:56:33 PM(UTC)
Sjarl

Rank: Sienn Developer

Reputation:

Groups: Administration
Joined: 10/8/2008(UTC)
Posts: 23

Simple programs need often to read/writte configurations settings. Mostlikely in a xml file.
A simple way to do this, is to create a configuration class, which can read/write from to an xml file.
For this the class below is easy to use, where you can change the base class ConfigSettings to your own needed settings. Very nice is the 1 to many list of children in this class.

Quote:

Imports System.IO
Imports System.Xml.Serialization

<Serializable()> _
Public Class ConfigSettings
Public FirstName As String
Public LastName As String
Public Children As List(Of Child)

Public Sub New()
Children = New List(Of Child)
End Sub

Class Child
Public Name As String

Sub New()
End Sub

Sub New(ByVal ChildName As String)
Name = ChildName
End Sub
End Class

Public Function ReadSettingClassFromFile(ByVal XMLFileName As String, ByRef Settings As ConfigSettings, ByRef ErrorMessage As String) As Boolean
Dim RetValue As Boolean = False
Try
'Read xml file into string
Dim xml As String = ""
Dim objReader As StreamReader
objReader = New StreamReader(XMLFileName)
xml = objReader.ReadToEnd
objReader.Close()

Dim serializer As XmlSerializer = New XmlSerializer(Settings.GetType)
Dim sr As StringReader = New StringReader(xml)
Settings = CType(serializer.Deserialize(sr), ConfigSettings)
RetValue = True
Catch ex As Exception
ErrorMessage = ex.ToString
RetValue = False
End Try
Return RetValue
End Function

Public Function WriteSettingClassFromFile(ByVal XMLFileName As String, ByVal Settings As ConfigSettings, ByRef ErrorMessage As String) As Boolean
Dim RetValue As Boolean = False
Try
' Serialize class into xml file
Dim serializer As XmlSerializer = New XmlSerializer(GetType(ConfigSettings))
Dim sw As StringWriter = New StringWriter()
serializer.Serialize(sw, Settings)
sw.Close()
Dim XML As String = sw.GetStringBuilder().ToString()

' Save to XML file
Dim objReader As StreamWriter
objReader = New StreamWriter(XMLFileName)
objReader.Write(XML)
objReader.Close()
RetValue = True
Catch ex As Exception
ErrorMessage = ex.ToString
RetValue = False
End Try
Return RetValue
End Function

End Class


This class can be use for reading like:

Quote:

Dim ErrorMessage As String = ""
Dim Settings As New ConfigSettings
If Settings.ReadSettingClassFromFile("ConfigSettings.xml", Settings, ErrorMessage) Then
LbOutput.Text = Settings.FirstName & " " & Settings.LastName & vbCrLf
For Each child As ConfigSettings.Child In Settings.Children
LbOutput.Text &= "- " & child.Name & vbCrLf
Next
Else
LbOutput.Text = ErrorMessage
End If


And for writing like:

Quote:

Dim Settings As ConfigSettings = New ConfigSettings()
Settings.FirstName = "Erik"
Settings.LastName = "Van Buren"
Settings.Children.Add(New ConfigSettings.Child("Jan"))
Settings.Children.Add(New ConfigSettings.Child("Mike"))
Settings.Children.Add(New ConfigSettings.Child("Jason"))

Dim ErrorMessage As String = ""

If Settings.WriteSettingClassFromFile("ConfigSettings.xml", Settings, ErrorMessage) Then
LbOutput.Text = "File written"
Else
LbOutput.Text = ErrorMessage
End If

Wanna join the discussion?! Login to your Sienn Forum forum account. New Registrations are disabled.

Users browsing this topic
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.