Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将文件转换为二进制文件以插入xml文件_Xml_Binary_Attachment - Fatal编程技术网

将文件转换为二进制文件以插入xml文件

将文件转换为二进制文件以插入xml文件,xml,binary,attachment,Xml,Binary,Attachment,如果我查看MS Infopath XML文件的内部,附件似乎是存储在标记之间的某种ASCII二进制代码。我没有背景资料,所以我不确定。我想用VBA应该很容易生成 下面是我正在使用的代码。在本例中,我试图从Excel文件中获取可以嵌入的文本。如果运行该代码,则错误应该与参数类型错误有关。在本例中,我引用了一个Excel文件,但它可能有很多内容 标记之间的代码类似于:stu2zaqvbfipwi8bhadfcikwniosxnma8qfeqwptuwyr5plr1p77rqg4mqlhimoczuw

如果我查看MS Infopath XML文件的内部,附件似乎是存储在标记之间的某种ASCII二进制代码。我没有背景资料,所以我不确定。我想用VBA应该很容易生成

下面是我正在使用的代码。在本例中,我试图从Excel文件中获取可以嵌入的文本。如果运行该代码,则错误应该与参数类型错误有关。在本例中,我引用了一个Excel文件,但它可能有很多内容

标记之间的代码类似于:
stu2zaqvbfipwi8bhadfcikwniosxnma8qfeqwptuwyr5plr1p77rqg4mqlhimoczuwurhjmdppanvxtjc4ei

我不知道这是否有用,但我不知道该怎么做

Const adTypeBinary = 1Const adTypeText = 2
Const adModeReadWrite = 3


Sub RunThis()
    bin2var "c:\documents\IYYMMCC Validation.xlsx"
End Sub


Function bin2var(filename As String) As String
    Dim f As Integer
    f = FreeFile()
    Open filename For Binary Access Read Lock Write As #f
        bin2var = Space(FileLen(filename))
        Get #f, , bin2var
        thestring = BytesToString(bin2var, CdoUS_ASCII)
    Close #f
End Function


Function BytesToString(bytes, charset)
    With CreateObject("ADODB.Stream")
        .Mode = adModeReadWrite
        .Type = adTypeBinary
        .Open
        .Write bytes
        .Position = 0
        .Type = adTypeText
        .charset = charset
        BytesToString = .ReadText
    End With
End Function
标签之间的代码

。。。很有可能。
因此,使用Base64编码器将二进制文件编码为Base64格式,然后将该字符串包含到XML文件中


假设您使用VisualBasic,快速搜索显示。

以下是我找到的解决方案。我使用它将文件转换为本例中所需的Base64:

Function EncodeBase64ForString(strString As String) As String
Dim arrData() As Byte

  arrData = StrConv(strString, vbFromUnicode)

  Dim objXML As MSXML2.DOMDocument60
  Dim objNode As MSXML2.IXMLDOMElement

  Set objXML = New MSXML2.DOMDocument60
  Set objNode = objXML.createElement("b64")

  objNode.DataType = "bin.base64"
  objNode.nodeTypedValue = arrData
  EncodeBase64ForString = objNode.text

  Set objNode = Nothing
  Set objXML = Nothing
End Function

Public Function GetFileBytes(ByVal strPath As String) As Byte()
Dim lngFileNum As Long
Dim bytRtnVal() As Byte

    lngFileNum = FreeFile

    If LenB(Dir(strPath)) Then ''// Does file exist?
        Open strPath For Binary Access Read As lngFileNum
        ReDim bytRtnVal(LOF(lngFileNum) - 1&) As Byte
        Get lngFileNum, , bytRtnVal
        Close lngFileNum
    Else
        Err.Raise vbObjectError + 1, , "Could not find " & strPath
    End If

    GetFileBytes = bytRtnVal

    Erase bytRtnVal
End Function

这很有帮助。我使用的是VBA,而不是VB,因此链接的解决方案无法工作。我确实找到了完成大部分工作所需的代码,但我仍然需要一些东西将以字符串形式读入的文件转换为字节,就像某种BinToByte例程一样。我还没有找到一个有效的。下面是一个PDF文件示例。%1.1.7 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 Base64编码的一种解决方案。另一种解决方案(“Aufruf”的意思是“调用它”)。