Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleSerialize and deserialize objects in VB6
Keywordsserialize, deserialize, XML, typelib, type information
CategoriesAPI
 
The XML tools provided by VB .NET allow you to easily serialize and deserialize objects. In this example, Toby shows how to do something similar using VB 6.

First, select the Project menu's References command. Select the Typelib Information entry and click OK. Now you can use the TLI functions to get typelib information about objects defined by the program.

The following code loops through an object's members and builds an XML serialization representing the object. Note that this routine does not need to know what fields the object contains. It uses TLI objects and routines to figure that out.

 
Private Function myXMLProperties(ByVal Object As Object, _
    Optional XMLString As String = "", Optional ByVal _
    AsAttributes = True, Optional ByVal OmitProperties As _
    String = "XMLProperties") As String
  Dim tTLI      As TLIApplication
  Dim tMem      As MemberInfo
  Dim tDom      As DOMDocument
  Dim tNode     As IXMLDOMNode
  Dim tInvoke   As InvokeKinds
  Dim tOmit     As String
  Dim tName     As String     'used as lower case....
  Dim tString   As String

  Set tTLI = New TLIApplication
  Set tDom = New DOMDocument
  
  If Len(XMLString) Then
  
  '... if string given, then we are letting new property
  ' values from xmlstring
      tInvoke = VbLet
      tDom.loadXML (XMLString)
  
  Else
  
  '... else we are getting existing property values
      tInvoke = VbGet
      tDom.appendChild tDom.createNode(NODE_ELEMENT, _
          TypeName(Object), "")
  
  End If
  
  tOmit = "," & LCase(OmitProperties) & ","
  
  '... handle each get or let member from object
  
  For Each tMem In _
      TLI.InterfaceInfoFromObject(Object).Members
      
      tName = LCase(tMem.Name)
      
'      Debug.Print tName, tMem.InvokeKind
      
      '... get or let and not omitted property
      '    for example object etc...
      
      If tMem.InvokeKind = tInvoke And InStr(tOmit, "," & _
          tName & ",") = 0 _
         And tMem.Parameters.Count = 0 Then
           
'           If tMem.ReturnType.VarType = VT_DISPATCH Then
'            '.. do nothing or do it recursive
'           ElseIf tMem.ReturnType.VarType = VT_ARRAY Then
'            '.. do nothing or do it somehow else
'           End If
           
          On Error Resume Next  'could be object or
              ' something else that can't handle
          If tInvoke = VbGet Then
             '... put data to XML-node
             If AsAttributes Then
               Set tNode = tDom.createAttribute(tName)
               tNode.Text = CallByName(Object, tMem.Name, _
                   VbGet)
               tDom.documentElement.Attributes.setNamedItem _
                   tNode
             Else
               Set tNode = tDom.createElement(tName)
               tNode.Text = CallByName(Object, tMem.Name, _
                   VbGet)
               tDom.documentElement.appendChild tNode
             End If
          Else
             '... get data from XML-node
             If AsAttributes Then
               CallByName Object, tMem.Name, VbLet, _
                 tDom.documentElement.Attributes.getNamedItem(tName).Text
             Else
               CallByName Object, tMem.Name, VbLet, _
                 tDom.documentElement.selectSingleNode(tName).Text
             End If
          End If
          On Error GoTo 0
      End If
  Next
  
  myXMLProperties = tDom.xml

End Function
 
The tools provided by VB .NET make this easier, but this method is quite powerful.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated