VB6 String to XML UTF8

peters4286 used Ask the Experts™
I'm working on a program written in VB6 that outputs a few xml files.
Presently, the xml files are built up by simply writing text to a text file then saving as .xml
This has worked for many years wihtout a hitch.
Recently we have partnered with a company that wants to import our files but they must be encoded in UTF8.

I have read many articles found on EE and google but can't quite get my head around it all.
This link for example:
Shows some code to convert a string to UTF8, but the result I get is just a series of question marks and odd characters.  The example UTF8 xml files the other company have provided show normal text in the xml file.

Should I be converting to UTF8, then back to a string ?

Obviously Im quite a novice when It comes to this, so any help is much appreciated.
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Hello peters4286,

Take a look:

Save a file in UTF-8



You must include the encoding directive at the begining of your XML document so the reader knows how to interpret it, i.e.

<?xml version="1.0" encoding="UTF-16"?>

From VB you can use the StrConv function to convert you vb string.


Thanks Rimvis, the following code found in the link you provided worked a treat !

Private Sub CreateFile(ByVal pstrFile As String, ByVal pstrData As String) 
  Dim objStream As Object 
  'Create the stream 
  Set objStream = CreateObject("ADODB.Stream") 
  'Initialize the stream 
  'Reset the position and indicate the charactor encoding 
  objStream.Position = 0 
  objStream.Charset = "UTF-8" 
  'Write to the steam 
  objStream.WriteText pstrData 
  'Save the stream to a file 
  objStream.SaveToFile pstrFile 
End Sub

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial