「XMLDOMDocument に XML 宣言を含めるにはどうすればよいですか?」で見られるコードのバリアントを使用しています。(これはMSDNでも見ることができます。エンコーディングを「UTF-16」に変更すると、UTF-16 として出力されると思われます...そして、テキスト エディターで出力を見ると「そうします」... ; しかし、16 進エディタでチェックすると、(プロパティが true に設定されているにもかかわらず) バイト オーダー マークが欠落しており、XML エディタは BOM が欠落しているためにドキュメントを無効な UTF-16 として拒否します。
私は何を見落としていますか?
'' # Create and load a DOMDocument object.
Dim xmlDoc As New DOMDocument60
xmlDoc.loadXML("<doc><one>test1</one><two>test2</two></doc>")
'' # Set properties on the XML writer - including BOM, XML declaration and encoding
Dim wrt As New MXXMLWriter60
wrt.byteOrderMark = True
wrt.omitXMLDeclaration = False
wrt.encoding = "UTF-16"
wrt.indent = False
'' # Set the XML writer to the SAX content handler.
Dim rdr As New SAXXMLReader60
Set rdr.contentHandler = wrt
Set rdr.dtdHandler = wrt
Set rdr.errorHandler = wrt
rdr.putProperty "http://xml.org/sax/properties/lexical-handler", wrt
rdr.putProperty "http://xml.org/sax/properties/declaration-handler", wrt
'' # Now pass the DOM through the SAX handler, and it will call the writer
rdr.parse xmlDoc
'' # Let the writer do its thing
Dim iFileNo As Integer
iFileNo = FreeFile
Open App.Path + "\saved.xml" For Output As #iFileNo
Print #iFileNo, wrt.output
Close #iFileNo
出力は次のようになります。
<?xml version="1.0" encoding="UTF-16" standalone="no"?>
<doc><one>test1</one><two>test2</two></doc>
なぜ VB6 を使用しているのですか? 実際には VBA (同世代、VB6 のわずかなサブセット) であり、EMC-Captiva の InputAccel/FormWare のスクリプト言語として使用されるため、切り替えはオプションではありません。