Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

COM Object Serialization

0.00/5 (No votes)
21 Oct 2003 1  
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 reference
  • Serialize: Serializes an object into an XML document

Public Properties

  • CollectionAddMethod: The name of the collection method that will return a reference to a collection item
  • IgnoreList: Semicolon delimited list of properties to ignore when serializing
  • LastError: 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

  1. Arrays and User Defined Types not supported
  2. Child objects will deserialize only if the property returns a valid reference to the object
  3. 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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here