DotNet serialization is really easy to use. I've come to that conclusion lately, after converting a event logging class that I use to make it message queue compatable. Before converting the class I did a bit of research and found that Dot Net serialization gets mixed reviews, mostly it seems, because of how it handles (or fails to handle) complicated classes. But my class is simple and for me it has worked just fine.
Serialization is the process of converting an object into some sort of generic format (say, for example, XML), then converting it back again into the object. This allows objects to be stored in databases (or message queues).
Here's what I had to do to make my event class serializable:
1) I added the <Serializable> tag above the declaration of the class that I wished to serialize, as shown here:
2) I then created a function to serialize the object. Objects can be serialized to streams such as text streams or memory streams. I just want to create a standard XML version of the object, so a memory stream works fine for me here. Once I've serialized the object to the memory stream, I then need to convert the contents of the memory stream to a string. Thus the reference to ASCIIEncoding.
The function that performs the serialization looks like this:
Here's what an _event class that's been serialized using the above function looks like:
3) To deserialize the XML back into the class, the string has to be converted back into a memory stream and the serializer has to be told what type of class to generate. Then just call "Deserialize" an you're done! The subroutine to unserialize the object looks like this:
Serialization is the process of converting an object into some sort of generic format (say, for example, XML), then converting it back again into the object. This allows objects to be stored in databases (or message queues).
Here's what I had to do to make my event class serializable:
1) I added the <Serializable> tag above the declaration of the class that I wished to serialize, as shown here:
<Serializable()> _
Public Class _Event
'---class body goes here
End Class
2) I then created a function to serialize the object. Objects can be serialized to streams such as text streams or memory streams. I just want to create a standard XML version of the object, so a memory stream works fine for me here. Once I've serialized the object to the memory stream, I then need to convert the contents of the memory stream to a string. Thus the reference to ASCIIEncoding.
The function that performs the serialization looks like this:
Protected Function SerializeMe(ByVal oObject As _Event) As String
Dim oMS As New MemoryStream
Dim oSXML As New System.Xml.Serialization.XmlSerializer(GetType(_Event))
oSXML.Serialize(oMS, oObject)
Dim oEncoding As System.Text.ASCIIEncoding = New System.Text.ASCIIEncoding
Dim sXML As String = oEncoding.GetString(oMS.GetBuffer)
Return sXML
End Function
Here's what an _event class that's been serialized using the above function looks like:
"<?xml version="1.0"?>
<_Event xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ID>-1</ID>
<Application>testapp</Application>
<Procedure>testproc</Procedure>
<Exception>myexception</Exception>
<Source>testsource</Source>
<CreateDate>7/14/2006 4:23:39 PM</CreateDate>
</_Event>"
3) To deserialize the XML back into the class, the string has to be converted back into a memory stream and the serializer has to be told what type of class to generate. Then just call "Deserialize" an you're done! The subroutine to unserialize the object looks like this:
Protected Sub DeserializeMe( _
ByVal sXML As String, _
ByRef oObject As _Event)
Dim oEncoding As System.Text.ASCIIEncoding = New System.Text.ASCIIEncoding
Dim oByteArray As Byte() = oEncoding.GetBytes(sXML)
Dim oMS As New MemoryStream(oByteArray)
Dim oSXML As New System.Xml.Serialization.XmlSerializer(GetType(_Event))
oObject = oSXML.Deserialize(oMS)
End Sub
0 Comments:
Post a Comment
<< Home