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
Set oProject = New Project
With oProject
.ProjectName = "Test"
.ProjectID = 1
.ProjectDescription = "This is a test"
.TestDate = Now
Set oUser = .Users.Add
oUser.FirstName = "Me"
oUser.LastName = "Moe"
oUser.Email = "me@moe.com"
oUser.Phone = "555-1212"
Set oUser = Nothing
Set oTask = .Tasks.Add
oTask.TaskID = "123"
oTask.TaskName = "test task"
oTask.User.FirstName = "Test"
oTask.User.LastName = "Person"
Set oTask = Nothing
Set oTask = .Tasks.Add
oTask.TaskID = "222"
oTask.TaskName = "second test task"
oTask.User.FirstName = "Second"
oTask.User.LastName = "Person"
Set oTask = Nothing
End With
Set oXmlSerializer = New ComSerialization.XmlSerializer
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 Set oProject = New Project
oXmlSerializer.Deserialize oProject, sXML
Conclusion
Hope you find this component useful.
Paul Welter
http://www.loresoft.com
History
- 21st October, 2003: Initial post