F
Farouche
Hi all
I have made two simple methods to serialize/deserialize a simple data
structure to my database using SoapFormatting.
This actually works just great, when the structures stays the same. BUT if I
change the structure, say adds a field, I get an error when trying to
deserialize the allready serialized object from the database saying that the
number of fields of the structure does not match the number of fields in the
one I'm trying to deserialize.
Thids is quite a problem since I'm not able to make any future changes to my
data.
Does anyone know how to get about this problem ??
Thanks in Advance
Farouche
Public Shared Function StreamToString(ByVal st As System.IO.Stream) As
String
st.Position = 0
If st.CanRead And st.CanSeek Then
Dim by(Convert.ToInt32(st.Length - 1)) As Byte
st.Read(by, 0, by.Length)
st.Position = 0
Return System.Text.Encoding.UTF8.GetString(by)
End If
End Function
Public Shared Function StringToStream(ByVal s As String) As System.IO.Stream
Dim by(s.Length - 1) As Byte
by = System.Text.Encoding.UTF8.GetBytes(s)
Dim st As System.IO.Stream = New System.IO.MemoryStream(s.Length)
st.Write(by, 0, by.Length)
st.Position = 0
Return st
End Function
Public Shared Function ObjToXml(ByVal obj As Object) As String
Dim Stream As System.IO.Stream = New System.IO.MemoryStream()
Dim SoapFormatter As System.Runtime.Serialization.IFormatter = New
System.Runtime.Serialization.Formatters.Soap.SoapFormatter()
Dim SoapString As String
SoapFormatter.Serialize(Stream, obj)
SoapString = StreamToString(Stream)
Stream.Close()
Stream = Nothing
Return SoapString
End Function
Public Shared Function XmlToObj(ByVal Xml As String) As Object
Dim SoapFormatter As System.Runtime.Serialization.IFormatter = New
System.Runtime.Serialization.Formatters.Soap.SoapFormatter()
Dim Stream As System.IO.Stream = StringToStream(Xml)
Dim obj As Object
obj = SoapFormatter.Deserialize(Stream)
Stream.Close()
Stream = Nothing
Return obj
End Function´
I have made two simple methods to serialize/deserialize a simple data
structure to my database using SoapFormatting.
This actually works just great, when the structures stays the same. BUT if I
change the structure, say adds a field, I get an error when trying to
deserialize the allready serialized object from the database saying that the
number of fields of the structure does not match the number of fields in the
one I'm trying to deserialize.
Thids is quite a problem since I'm not able to make any future changes to my
data.
Does anyone know how to get about this problem ??
Thanks in Advance
Farouche
Public Shared Function StreamToString(ByVal st As System.IO.Stream) As
String
st.Position = 0
If st.CanRead And st.CanSeek Then
Dim by(Convert.ToInt32(st.Length - 1)) As Byte
st.Read(by, 0, by.Length)
st.Position = 0
Return System.Text.Encoding.UTF8.GetString(by)
End If
End Function
Public Shared Function StringToStream(ByVal s As String) As System.IO.Stream
Dim by(s.Length - 1) As Byte
by = System.Text.Encoding.UTF8.GetBytes(s)
Dim st As System.IO.Stream = New System.IO.MemoryStream(s.Length)
st.Write(by, 0, by.Length)
st.Position = 0
Return st
End Function
Public Shared Function ObjToXml(ByVal obj As Object) As String
Dim Stream As System.IO.Stream = New System.IO.MemoryStream()
Dim SoapFormatter As System.Runtime.Serialization.IFormatter = New
System.Runtime.Serialization.Formatters.Soap.SoapFormatter()
Dim SoapString As String
SoapFormatter.Serialize(Stream, obj)
SoapString = StreamToString(Stream)
Stream.Close()
Stream = Nothing
Return SoapString
End Function
Public Shared Function XmlToObj(ByVal Xml As String) As Object
Dim SoapFormatter As System.Runtime.Serialization.IFormatter = New
System.Runtime.Serialization.Formatters.Soap.SoapFormatter()
Dim Stream As System.IO.Stream = StringToStream(Xml)
Dim obj As Object
obj = SoapFormatter.Deserialize(Stream)
Stream.Close()
Stream = Nothing
Return obj
End Function´