3 Replies - 10254 Views - Last Post: 06 June 2010 - 11:43 AM Rate Topic: -----

#1 Blues End  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 42
  • Joined: 12-June 09

Serialize a Dataset

Posted 05 June 2010 - 05:22 PM

I thought I had mastered how to serialize and desrialize but I can't figure this out:
In my project I have 4 datatables in a dataset and I wish to serialize the dataset. Everything seems to work ok ie no errors or exceptions - except that no rows appear in the datables after the deserialize process.
Is thee anything special about a datasaet to serialize it??
Here is my code:
    Private Sub TSMI01c_SaveAs_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles TSMI01c_SaveAs.Click
        If DLG01_SaveFileDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
            Dim FFS As FileStream = File.Create("c:\Users\Steve\Documents\Routes\11.wpt")
            TheBF = New BinaryFormatter
            TheBF.Serialize(FFS, ds_WptDataset)
            FFS.Close()
        End If
    End Sub
'''''''''''''
    Private Sub TSMI01b_Open_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles TSMI01b_Open.Click
        If DLG02_OpenFileDialog.ShowDialog = Windows.Forms.DialogResult.OK Then
            Dim FSS As New FileStream("c:\Users\Steve\Documents\Routes\11.wpt", FileMode.Open)
            TheBF = New BinaryFormatter
            ds_WptDataset = CType(TheBF.Deserialize(FSS), DataSet)
            FSS.Close()
            MessageBox.Show(dt_Wpt.Rows.Count)
        End If
    End Sub


Any help would be appreciated, any advice etc
Regards,
Steve

This post has been edited by AdamSpeight2008: 05 June 2010 - 05:28 PM
Reason for edit:: Please remember to use the code tags.


Is This A Good Question/Topic? 0
  • +

Replies To: Serialize a Dataset

#2 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1162
  • View blog
  • Posts: 4,444
  • Joined: 02-July 08

Re: Serialize a Dataset

Posted 05 June 2010 - 06:05 PM

Why not use the dataset.WriteXML and dataset.ReadXML methods?
Was This Post Helpful? 0
  • +
  • -

#3 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1658
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Serialize a Dataset

Posted 05 June 2010 - 07:24 PM

I personally would serialize it to XML then back again. To serialize it as XML use the XmlSerializer's Serialize method, then to deserialize it use it's Deserialize method.

Here's an example of serializing a DataSet to XML

Public Sub SerializeDataset(ByRef ds As DataSet)
	Dim writer As New StreamWriter("SerializedDataset.xml")
	Dim serializer As New XmlSerializer(ds.GetType())
	
	serializer.Serialize(writer, ds)
	writer.Close()
	
	ds.Clear()
End Sub



And an example of Deserializing it

Public Function DeserializeDataSet(ByRef file As String) As DataSet
	Dim ds As New DataSet()
	Dim deSerializer As New XmlSerializer(ds.GetType())
	Dim stream As New FileStream(file, FileMode.Open)
	
	ds = CType(deSerializer.Deserialize(stream), DataSet)
	
	Return ds
End Function



Hope that helps :)

EDIT: Remember, when calling DeserializeDataSet provide the name you saved the data as when serializing it, which in the example above is SerializedDataset.xml
Was This Post Helpful? 0
  • +
  • -

#4 Blues End  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 42
  • Joined: 12-June 09

Re: Serialize a Dataset

Posted 06 June 2010 - 11:43 AM

Thanks guys, appreciate your advice and will try it- as a matter of interest, why do you favour xml?
Regards,
Steve

This post has been edited by Blues End: 06 June 2010 - 11:44 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1