Click here to Skip to main content
15,884,298 members
Articles / Programming Languages / VBScript

COM Object Serialization

Rate me:
Please Sign up or sign in to vote.
3.90/5 (7 votes)
21 Oct 2003CPOL2 min read 55.6K   14   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

VB.NET
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

VBScript
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

VB.NET
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

VB.NET
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Generalcollections Pin
Anonymous13-Aug-04 3:02
Anonymous13-Aug-04 3:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.