Serialization in vb2005
-
I have a class that I want to save to viewstate. In order to save it to viewstate it has to be serializable. How do you serialize a class and then unserialize it. thanks John
You mark it as serialisable, if it contains only POD, that is all you need, I believe.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog
-
I have a class that I want to save to viewstate. In order to save it to viewstate it has to be serializable. How do you serialize a class and then unserialize it. thanks John
This book shows you how to serialize a class and deserialize it. good book
-
I have a class that I want to save to viewstate. In order to save it to viewstate it has to be serializable. How do you serialize a class and then unserialize it. thanks John
John, public oObj as object . . end class Note: private variables will not be serialized Note: some variable types cannot be serialized Note: if you dont' want the value to be seralized you need to ingore the value Note: import these libraries Imports System.io Imports System.Xml Imports System.Xml.Serialization ------- To Serialize: Public Function sSerializeEntity(ByVal oEntity As Object) As String Dim oSerializer As New XmlSerializer(oEntity.GetType) Dim oSR As New MemoryStream, bt() As Byte Try oSerializer.Serialize(oSR, oEntity) bt = oSR.GetBuffer sSerializeEntity = System.Text.Encoding.ASCII.GetString(bt) oSR.Close() oSR = Nothing oSerializer = Nothing Catch ex As Exception MsgBox(ex.ToString) End Try End Function ----------- To Deserialize: Public Sub DeSerializeEntity(ByVal sEntity As String, ByRef oEntity As Object) Dim oSerializer As New XmlSerializer(oEntity.GetType) Dim oSR As MemoryStream Try oSR = New MemoryStream(System.Text.Encoding.ASCII.GetBytes(sEntity)) oEntity = oSerializer.Deserialize(oSR) oSR.Close() oSR = Nothing oSerializer = Nothing Catch ex As Exception MsgBox(ex.ToString) End Try End Sub --------------- ** to serlialize object ** Dim oMyEntity as new MyEntity oMyEntity.sName = "Test" dim sEntity As string = sSerializeEntity(oMyEntity) '''''''serialized''''''' you can pass the value of sEntity anywhere ** to put back into an object ** Dim oMyEntity as new MyEntity DeSerializeEntity(sEntity,oMyEntity) '''''''deserialized''''''' now you can use the object msgbox(oMyEntity.sName)
-
John, public oObj as object . . end class Note: private variables will not be serialized Note: some variable types cannot be serialized Note: if you dont' want the value to be seralized you need to ingore the value Note: import these libraries Imports System.io Imports System.Xml Imports System.Xml.Serialization ------- To Serialize: Public Function sSerializeEntity(ByVal oEntity As Object) As String Dim oSerializer As New XmlSerializer(oEntity.GetType) Dim oSR As New MemoryStream, bt() As Byte Try oSerializer.Serialize(oSR, oEntity) bt = oSR.GetBuffer sSerializeEntity = System.Text.Encoding.ASCII.GetString(bt) oSR.Close() oSR = Nothing oSerializer = Nothing Catch ex As Exception MsgBox(ex.ToString) End Try End Function ----------- To Deserialize: Public Sub DeSerializeEntity(ByVal sEntity As String, ByRef oEntity As Object) Dim oSerializer As New XmlSerializer(oEntity.GetType) Dim oSR As MemoryStream Try oSR = New MemoryStream(System.Text.Encoding.ASCII.GetBytes(sEntity)) oEntity = oSerializer.Deserialize(oSR) oSR.Close() oSR = Nothing oSerializer = Nothing Catch ex As Exception MsgBox(ex.ToString) End Try End Sub --------------- ** to serlialize object ** Dim oMyEntity as new MyEntity oMyEntity.sName = "Test" dim sEntity As string = sSerializeEntity(oMyEntity) '''''''serialized''''''' you can pass the value of sEntity anywhere ** to put back into an object ** Dim oMyEntity as new MyEntity DeSerializeEntity(sEntity,oMyEntity) '''''''deserialized''''''' now you can use the object msgbox(oMyEntity.sName)