XML as Database in Vb.Net
By: Jon Roberts
It is possible to store dataset tables in a xml for later retrieval. This useful for times when you need to store data but do not need the full functionality of a database engine, this also allows you to deploy application with out the need to ensure that the proper db engine is on the system.
The cool part is that the dataset will generate the xml file for you once you have coded the definition.
To work with data sets you need to insure that you project has reference to the System.IO portion of the .Net framework.
The below code is a sample of setting up the tables for the dataset to be written out to an xml file.
This example is for a dtFolders and dtObject Tables and the Dataset of dsStuff
Imports System
Imports System.IO
Public Class Example
Dim dtFolders As DataTable
Dim dtObjects As DataTable
Dim dsStuff as Dataset
Public Sub Define_Tables()
Try
dtFolders = New DataTable("Folders")
dtObjects = New DataTable("Objects")
'Folders
Dim dcID As DataColumn = New DataColumn("ID")
dcID.DataType = System.Type.GetType("System.Int32")
dtFolders.Columns.Add(dcID)
Dim dcName As DataColumn = New DataColumn("Name")
dcName.DataType = System.Type.GetType("System.String")
dtFolders.Columns.Add(dcName)
Dim dcParentID As DataColumn = New DataColumn("ParentID")
dcParentID.DataType = System.Type.GetType("System.Int32")
dtFolders.Columns.Add(dcParentID)
'Objects
Dim dcOID As DataColumn = New DataColumn("ID")
dcOID.DataType = System.Type.GetType("System.Int32")
dtObjects.Columns.Add(dcOID)
Dim dcOName As DataColumn = New DataColumn("Name")
dcOName.DataType = System.Type.GetType("System.String")
dtObjects.Columns.Add(dcOName)
Dim dcOParentID As DataColumn = New DataColumn("ParentID"
dcOParentID.DataType = System.Type.GetType("System.Int32")
dtObjects.Columns.Add(dcOParentID)
Dim dcOType As DataColumn = New DataColumn("ObjectType")
dcOType.DataType = System.Type.GetType("System.String")
dtObjects.Columns.Add(dcOType)
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Error")
End Try
End Sub
I have found that the best data types to use are string and int32 when defining the data set that will be written out to xml. When you need to use the data later as a more complex type you can use the Ctype function to convert the string or int32 value to the appropriate type.
Here is example code of a functions for adding data to the the Folder and Object tables
Public Function AddFolder(ByVal fID As Integer, ByVal fName As String, ByVal fParent As Integer) As Boolean
Dim frow As DataRow
Try
frow = dtFolders.NewRow()
frow.Item("ID") = fID
frow.Item("Name") = fName
frow.Item("ParentID") = fParent
dtFolders.Rows.Add(frow)
Return True
Catch ex As Exception
Return False
End Try
End Function
Public Function AddObject(ByVal boID As Integer, ByVal boName As String, ByVal boParent As Integer, ByVal boType As String) As Boolean
Dim orow As DataRow
Try
orow = dtObjects.NewRow()
orow.Item("ID") = boID
orow.Item("Name") = boName
orow.Item("ParentID") = boParent
orow.Item("ObjectType") = boType
dtObjects.Rows.Add(orow)
Return True
Catch ex As Exception
Return False
End Try
End Function
It is even easier to add the tables to the data set
Public Function MakeDataset() As Boolean
Try
dsStuff = New DataSet
dsStuff.Tables.Add(dtFolders)
dsStuff.Tables.Add(dtObjects)
Return True
Catch ex As Exception
End Try
End Function
Writing out the Dataset to xml is also very easy. The path out is the location and name of the xml file to write out. You always want to use the XmlWriteMode.WriteSchema argument as this will make it much easier when reading the data back in to the tables.
Public Function DSWriteXML(ByVal Pathout As String) As Boolean
Try
dsStuff.WriteXml(Pathout, XmlWriteMode.WriteSchema)
Return True
Catch ex As Exception
Return False
End Try
End Function
When reading in the data from an xml you will not need to define the tables first. The definition is generated from the xml while the data is being read in.
Public Function ReadDef(ByVal strPath As String) As Boolean
Try
dsStuff = New DataSet ' destroy old instance to ensure no confilcts
dsstuff.ReadXml(strPath) ' strPath is the location and name of xml file to read in
Return True
Catch ex As Exception
Return False
End Try
End Function
You can use a Dataview to filter and sort the data from the loaded table.
Dim dvFolder as Dataview
Public Sub GetFolders( strFolderName as string)
try
dvFolder = dsStuff.tables("Folders")
dvFolder.sort = "Name"
dvFolder.RowFilter = strFolderName
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Error")
End Try
You can have the basic functionality of a Database and use the internal .Net data handle methods to store, retrieve, sort, and filter the data to and from an xml file with minimal overhead in your application.
This can useful for storing basic data or for storing complex configuration information .
Keep coding and testing the limits of your skill,
Jon Roberts