VB/VBA 中的 XML 反序列化

发布于 2024-08-30 09:04:42 字数 688 浏览 6 评论 0原文

我在 MS Access 数据库中有一组 VBA 类。 我有一个 xml 字符串,其中包含我想用来创建新类的数据。

除了单独设置每个属性之外,是否有一种简单的方法可以将 XML 反序列化到我的对象中?

我已经看到使用 TypeLib 库的代码

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

,但这似乎不适用于标准类模块。我收到 429 错误:

ActiveX Component Cannot Be Created

还有其他人可以帮助我吗?如果可以的话,我宁愿不必手动设置每个属性,其中一些类非常巨大!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

魔法唧唧 2024-09-06 09:04:42

您永远不会在该代码中实例化 tTLI 。然后将其称为 TLI 所以它不会工作,429错误可能是因为 TypeInfo 库未注册,您是否将其添加为引用?

如果您执行了以下操作,则可以:

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

如果您愿意,可以将 InvokeHook 替换为 CallByName

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文