【问题标题】:Base64 decode XML VBABase64 解码 XML VBA
【发布时间】:2022-01-10 11:58:06
【问题描述】:

我正在尝试在 VBA 中对 XML 文件进行 base64 解码,但在使用此函数时出现此错误:

该函数可用于解码普通文本字符串,但在尝试使用它解码 XML 文件时,出现该错误。任何人都知道我应该修改什么以使其正常工作?

Function Base64Decode(ByVal vCode)
    Set oNode = 
    CreateObject("Msxml2.DOMDocument.3.0").CreateElement("base64")
    oNode.dataType = "bin.base64"
    oNode.Text = vCode
    Base64Decode = oNode.nodeTypedValue
    Set oNode = Nothing
End Function

【问题讨论】:

  • 您是否对讨论中的文件进行编码?使用与文本文件相同的功能?如果不是,恐怕不是解码功能有问题,因为它是......也许它的编码是问题。
  • XML 文件是否可能包含任何 UTF-8 字符?
  • @FaneDuru 我使用 powershell 对其进行了编码: $cmd =@' xml pasteed here '@ [Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes($cmd) )
  • 尝试使用“ADODB.Stream”和“Microsoft.XMLDOM”进行编码/解码。我将发布功能。我不是他们的父亲,我在网上找到了他们......
  • 试试这个answer中的功能

标签: xml vba base64


【解决方案1】:

请尝试使用下一个函数进行编码/解码:

Function EncodeBase64(text$)
    Dim b
    With CreateObject("ADODB.Stream")
        .Open: .Type = 2: .Charset = "utf-8"
        .WriteText text: .Position = 0: .Type = 1: b = .Read
        With CreateObject("Microsoft.XMLDOM").createElement("b64")
            .DataType = "bin.base64": .nodeTypedValue = b
            EncodeBase64 = Replace(Mid(.text, 5), vbLf, "")
        End With
        .Close
    End With
End Function

Function DecodeBase64(b64$)
    Dim b
    With CreateObject("Microsoft.XMLDOM").createElement("b64")
        .DataType = "bin.base64": .text = b64
        b = .nodeTypedValue
        With CreateObject("ADODB.Stream")
            .Open: .Type = 1: .Write b: .Position = 0: .Type = 2: .Charset = "utf-8"
            DecodeBase64 = .ReadText
            .Close
        End With
    End With
End Function

【讨论】:

  • 如何使用该函数对我的 XML 进行编码?不知道如何将多行 XML 内容存储在 VBA 中。这就是我使用 powershell oneliner 的原因,因为它非常易于使用和编码
猜你喜欢
  • 2020-07-23
  • 2013-05-27
  • 2013-02-27
  • 1970-01-01
  • 1970-01-01
  • 2010-12-18
  • 1970-01-01
  • 1970-01-01
  • 2021-11-17
相关资源
最近更新 更多