No database - serialize and deserialize from XML

Ultrawhack

Well-known member
Joined
Jul 5, 2006
Messages
164
Location
Canada
Programming Experience
3-5
dellman said:
For a one user one database system I actually do not use a database at all. Depends on the number of tables but if you have only a couple of tables and no relationships to speak of (other than those you maintain by virtue of design) then why not use a DataSet object that you serialize and deserialize from XML? It is simple, elegent and requires absolutely nothing more than .NET. Also, you can alter the structure at will and when you deserialize an older object you can detect if it has the correct structure and if not, just update it so that when you serialize it again, it has the correct structure. You will not run into deserialization headache unless you try to use a Typed Dataset. You have nothing to install (ie: SSX, jet drivers,etc.), You have no services to run (ie: SSX), no memory overhead, the serialization and deserialization process is fast, ie: 1000 records in less than a second. Comments anyone? If you need working code on the serialization and deserialization for .NET 2005 I can post it.

Cheers.

I'd certainly like to study the code. Thanks Dellman
 
How to Serialize and Deserialize in VB.NET 2005 (Datasets)

The code for vb.net2005 for Serialize is:
It takes in two parameters and makes the assumption that your custom type class has a default constructure (ie: Sub New() with no params) and the outputfilename has already been checked out (verified that the path exists, but the file does not, or if it does you want it overwritten).

The following method the deserialize uses a try..catch because many more things can go wrong. Of course you should use a try catch on the serialize method as well, I just did not do it for this example.

VB.NET:
Class Serializer
    Public Shared Sub SerializeDataSetToFile(ByVal MyServer As DataSet, ByVal OutputFileName As String)
        Dim Serializer As New XmlSerializer(GetType(DataSet))
        Dim OutPut As Xml.XmlWriter
        OutPut = Xml.XmlWriter.Create(OutputFileName)
        Serializer.Serialize(OutPut, MyServer)
        OutPut.Close()
    End Sub

    Public Shared Function DeSerializeDataSetFromFile(ByVal File As String) As DataSet
        Dim ReturnDataSet As New DataSet
        Dim DeSerializer As New XmlSerializer(GetType(DataSet))
        If IO.File.Exists(File) Then
            Try
                Dim Input As Xml.XmlReader
                Input = Xml.XmlReader.Create(File)
                ReturnDataSet = DeSerializer.Deserialize(Input)
                Input.Close()
            Catch ex As Exception
                Throw ex
            End Try
        Else
            Throw New Exception("File Does Not Exist")
        End If
        Return ReturnDataSet
    End Function
End Class

Now, the argument can be made that you do not have an Add method and so forth that makes dealing with a regular database so much nicer and that is true. Please note that this method is to be used for extremely simple database applications such as Grandmas' Record Collection and such. If you use it for much more you will start running into brick walls in many different ways. Please note that this method can be used to serialize and deserialize just about any type of class with the exception of those that implement IDictionary interface (at least that was a limitation in .net 2003, I duuno if ms changed that or not as I have not tried) I generally use this for dataset work for apps that get get away with having NO RELATIONSHIPS and have limited storage needs. As soon as you require the power of relationships and such you need to consider a real data engine.

Comments?
 
Back
Top