COM Object Serialization






3.90/5 (7 votes)
Serializes and deserializes COM objects into and from XML documents
Introduction
The ComSerialization
component can be used to serialize and deserialize COM objects into and from XML documents. Serialization is the process of converting an object's public
properties and fields to a serial format (in this case, XML) for storage or transport. Deserialization restores the object to its original state from the XML output. You can think of serialization as a way of saving the state of an object into a buffer.
XmlSerializer Object
Public Methods
Deserialize
: Deserializes an XML document to the object referenceSerialize
: Serializes an object into an XML document
Public Properties
CollectionAddMethod
: The name of the collection method that will return a reference to a collection itemIgnoreList
: Semicolon delimited list of properties to ignore when serializingLastError
: The description of the last error that occurred
Serialize Method
Description
Serializes an object into an XML document
Syntax
Public Function Serialize( _
ByVal SourceObject As Object _
) As String
Parameters
SourceObject
A reference to an object to be serialized
Return Type
Returns an XML string representing the serialization of the object
Remarks
The serialize
method will serialize any readable property that does not have any parameters. If the property returns an object, it will be recursively serialized. Collections that support NewEnum
will also be serialized.
Use the IgnoreList
to exclude specific properties from serialization.
Warning: Be sure to ignore properties that could cause an infinite loop, like parent.
Serialization Limitations
Arrays and User Defined types are not supported.
Example
Dim oXmlSerializer As ComSerialization.XmlSerializer
Dim oProject As Project
Dim oTask As Task
Dim oUser As User
Dim sXML As String
'create object
Set oProject = New Project
'add some test data
With oProject
.ProjectName = "Test"
.ProjectID = 1
.ProjectDescription = "This is a test"
.TestDate = Now
'example collection
Set oUser = .Users.Add
oUser.FirstName = "Me"
oUser.LastName = "Moe"
oUser.Email = "me@moe.com"
oUser.Phone = "555-1212"
Set oUser = Nothing
'example collection
Set oTask = .Tasks.Add
oTask.TaskID = "123"
oTask.TaskName = "test task"
'example child object
oTask.User.FirstName = "Test"
oTask.User.LastName = "Person"
Set oTask = Nothing
'example collection
Set oTask = .Tasks.Add
oTask.TaskID = "222"
oTask.TaskName = "second test task"
'example child object
oTask.User.FirstName = "Second"
oTask.User.LastName = "Person"
Set oTask = Nothing
End With
'create serialization object
Set oXmlSerializer = New ComSerialization.XmlSerializer
'return xml
sXML = oXmlSerializer.Serialize(oProject)
Debug.Print sXML
Deserialize Method
Description
Deserializes an XML document to the object reference.
Syntax
Public Function Deserialize( _
ByVal TargetObject As Object _
ByVal sXML As Object _
) As Boolean
Parameters
TargetObject
A reference to an object to be deserialized
sXML
XML string to deserialize to the object
Remarks
Deserialization Limitations
- Arrays and User Defined Types not supported
- Child objects will deserialize only if the property returns a valid reference to the object
- To deserialize a collection, the add method of the collection must return a valid reference to an object that has been added to the collection. The add method must not have any required parameters. Note that collection keys will be lost on deserialization. You may specify an alternate to the add method by setting
CollectionAddMethod
property.
Example
Dim oXmlSerializer As ComSerialization.XmlSerializer
Dim oProject As Project
Set oXmlSerializer = New ComSerialization.XmlSerializer 'create the XmlSerializer
Set oProject = New Project 'create a new object reference
'call deserialize passing in object reference and xml
'sXML is the xml string that was return from Serialize
oXmlSerializer.Deserialize oProject, sXML
'oProject's state should now be the same as it was when we serialized it
Conclusion
Hope you find this component useful.
Paul Welter
http://www.loresoft.com
History
- 21st October, 2003: Initial post