XML Deserialization in VB/VBA
-
01-10-2019 - |
Question
I have a set of VBA classes in an MS Access database. I have an xml string with data I want to create new classes with.
Other than setting each property individually, is there an easy way to deserialize the XML into my object?
I've seen the code using the TypeLib library
Public Sub ISerializable_Deserialize(xml As IXMLDOMNode)
Dim tTLI As TLIApplication
Dim tInvoke As InvokeKinds
Dim tName As String
Dim tMem As MemberInfo
tInvoke = VbLet
For Each tMem In TLI.ClassInfoFromObject(Me).Members
tName = LCase(tMem.Name)
CallByName Me, tMem.Name, VbLet, xml.Attributes.getNamedItem(tName).Text
Next tMem
End Sub
but this doesn't seem to work with the standard class modules. I get a 429 error:
ActiveX Component Cannot Be Created
Can anyone else help me out? I'd rather not have to set each propery by hand if I can help it, some of these classes are huge!
Solution
You never instance tTLI
in that code & and later refer to it as just TLI
so it wont work, the 429 error may be because the TypeInfo library isn't registered, did you add it as a reference?
If you did the following will work:
Dim TLI As TLIApplication
Dim II As InterfaceInfo
Dim MI As MemberInfo
Set TLI = New TLIApplication
Set II = TLI.InterfaceInfoFromObject(Me)
For Each MI In II.Members
If MI.InvokeKind = InvokeKinds.INVOKE_PROPERTYPUT Then
Debug.Print MI.Name
TLI.InvokeHook Me, MI.Name, InvokeKinds.INVOKE_PROPERTYPUT, "PROPVALUE"
End If
Next
You can replace InvokeHook
with CallByName
if you wish.