'XMLSettings Class
'Date: February 10th, 2003
'Programmer: Jon Dick
'Website: [url]http://www.Net-Lutions.com[/url]
'
'Description:
' I designed this class to easily manage program settings in all of my windows and web applications. There are
'other INI XML file classes out there, but their code is more complicated though quite possibly more efficient.
'My goal was to make a simple class for this, no frills, just something that works, so here it is. At the same
'time, i learned a bit more about working with Datasets and XML, and so that was accomplished.
'Hopefully someone finds this useful, if not for the actual use, then for at least learning how to work with datasets
'and XML in a simple, quick, and clean fashion. Enjoy it!
'
'Oh yeah, go ahead and use this code for whatever you like. It took me an hour at most to make, so Use if freely!
Public Class XMLSettings
Public Shared Function Write(ByVal strFileName As String, ByVal strSetting As String, ByVal strValue As String) As Boolean
'This Function Takes a setting and it's corresponding value and writes it to the specified XML file
Dim dsSettings As DataSet
dsSettings = New DataSet()
If System.IO.File.Exists(strFileName) Then
'If a file already exists, load into the dataset
dsSettings.ReadXml(strFileName)
Else
'No file exists, so Create a new dataset
dsSettings.DataSetName = "XML_INI"
'Create Table in the dataset
dsSettings.Tables.Add("Settings")
'Create a new row, and add it to the table
Dim newRow As DataRow
newRow = dsSettings.Tables(0).NewRow
dsSettings.Tables(0).Rows.Add(newRow)
'Dispose of the datarow
newRow = Nothing
End If
'Now we are ready to make changes to the setting and its value
'Check to see if the setting we want exists yet or not
If dsSettings.Tables(0).Columns(strSetting) Is Nothing Then
'Setting doesn't exist, so create a new column for it
dsSettings.Tables(0).Columns.Add(strSetting)
End If
'Set the column value
dsSettings.Tables(0).Rows(0)(strSetting) = strValue
'Write the new changes to the xml file
dsSettings.WriteXml(strFileName)
'Dispose of the Dataset object immediately
dsSettings.Dispose()
End Function
Public Shared Function Read(ByVal strFileName As String, ByVal strSetting As String) As String
'This Function Reads A setting, returning its value (if it exists) from a specified XML File
Dim dsSettings As DataSet
dsSettings = New DataSet()
'Try Reading the XML File. If it doesn't exist, return a setting value of nothing, since the setting
'obviously also does not exist
Try
dsSettings.ReadXml(strFileName)
Catch
Read = ""
End Try
'If the file does exist, attempt to Read the Column (Setting) Value
'NOTE: The settings are always saved in the first row of the first table in the dataset
Try
Read = dsSettings.Tables(0).Rows(0)(strSetting)
Catch
Read = ""
End Try
'Dispose Immediately of the dataset
dsSettings.Dispose()
End Function
Public Shared Function Delete(ByVal strFileName As String, ByVal strSetting As String) As Boolean
'This Function Deletes A setting, returning a success boolean value
Dim dsSettings As DataSet
dsSettings = New DataSet()
'Try Reading the XML File. If it doesn't exist, return a setting value of nothing, since the setting
'obviously also does not exist
Try
dsSettings.ReadXml(strFileName)
Catch
'File doesn't exist, nothing to delete
Delete = False
Exit Function
End Try
'File Exists, so remove the column (setting) if it exists
Try
dsSettings.Tables(0).Columns.Remove(strSetting)
'Write Back to XML File
dsSettings.WriteXml(strFileName)
Delete = True
Catch
'Column Doesn't Exist
Delete = False
End Try
'Dispose Immediately of the dataset
dsSettings.Dispose()
End Function
End Class