打开一个 XML,读取文本,替换一些文本,将文件写回无 BOM 格式的 UTF-8

Open an XML, Read Text, Replace some Text, Write the file back in UTF-8 without BOM format

我的 IBM MQ 不接受 XML 以 UTF-8 格式保存的文件。我想试试它是否接受没有 BOM 格式的 UTF-8。我尝试了多种方法,但无法以非 BOM 格式保存文件。我的代码如下。

Dim objStreamUTF8      : Set objStreamUTF8      = CreateObject("ADODB.Stream")
Dim objStreamUTF8NoBOM : Set objStreamUTF8NoBOM = CreateObject("ADODB.Stream")

With objStreamUTF8
    .Charset = "UTF-8"
    .Mode = 3
    .Type = 2
    .Open
    objStreamUTF8.LoadFromFile uxtNewRenamePath'"C:\WINDOWS\Temp\DataFiles\TC10_ Apostrophe symbol in tag Cdtr_Adrline2_Pacs8_TxID201765133641.xml"
    objStreamUTF8.Flush
    strFileText = objStreamUTF8.ReadText()
    strFileText = Replace(strFileText,"&#", "&#")
    strFileText = Replace(Replace(Replace(Replace(Replace(strFileText, "GreaterThanSymbol", ">"), "LessThanSymbol", "<"), "ApostropheSymbol", "'"), "AmpersandSymbol", "&"), "DoubleQuotesSymbol", chr(34))
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    objFSO.DeleteFile uxtNewRenamePath, True
    Set objFSO = Nothing
    .Position = 0
    .Flush
    .WriteText strFileText
    .SaveToFile uxtNewRenamePath, 2
    .Close
    .Type = 1
    .Open
    .Position  = objStreamUTF8.Size
End With

With objStreamUTF8NoBOM
    .Mode = 3
    .Type = 1
    .Open
    objStreamUTF8.CopyTo objStreamUTF8NoBOM
    .SaveToFile uxtNewRenamePath, 2
End With

objStreamUTF8.Close
objStreamUTF8NoBOM.Close

objStreamUTF8NoBOM保存到文件时,文件是一片空白。能否请您告诉我我做错了什么或如何以所需格式保存文件?

知道了。第一个流的位置必须设置为第 3 个位置以跳过 BOM 字符

即objStreamUTF8.Position = 3