Lots of hubbub here about UTF-8, but the DOMDocument.save() method does use the PI to determine how to encode saved output. The only real snag is that for formatted output instead of economical output (no whitespace) you need to use the SAX Writer.
Basically things seem to work just as expected though. There is nothing hackish about this, it's how it is done.
Option Explicit
Private Sub Main()
Dim varStock As Variant
Dim docStock As MSXML2.DOMDocument
Dim elemRoot As MSXML2.IXMLDOMElement
Dim elemStock As MSXML2.IXMLDOMElement
Dim elemField As MSXML2.IXMLDOMElement
Dim I As Integer
varStock = Array(Array("12345", 10.32), _
Array("¥45632", 5.43)) 'Yen sign used here to show Unicode.
Set docStock = New MSXML2.DOMDocument
With docStock
.appendChild .createProcessingInstruction("xml", _
"version=""1.0"" encoding=""utf-8""")
Set elemRoot = .createElement("ArrayOfStock")
With elemRoot
.setAttribute "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"
.setAttribute "xmlns:xsd", "http://www.w3.org/2001/XMLSchema"
For I = 0 To UBound(varStock)
Set elemStock = docStock.createElement("Stock")
With elemStock
Set elemField = docStock.createElement("ProductCode")
elemField.Text = CStr(varStock(I)(0))
.appendChild elemField
Set elemField = docStock.createElement("ProductPrice")
elemField.Text = CStr(varStock(I)(1))
.appendChild elemField
End With
.appendChild elemStock
Next
End With
Set .documentElement = elemRoot
On Error Resume Next
Kill "created.xml"
On Error GoTo 0
.save "created.xml"
End With
End Sub
Examining the output file looking for the Yen sign you should see that the text is UTF-8 encoded.
If you want this in-memory rather than to disk you can .save() to something like an ADODB.Stream object, or just use XMLHTTPRequest.send with the DOMDocument as the argument (body). There is no need to resort to the heavyweight option of an Interop approach.