vbNewbie
vbNewbie

Reputation: 3345

converting byte() variable to string with encoding

I am consuming messages and the messages are stored in byte() format and I tried to convert it with proper encoding to a string but still see the unicode characters when writing it to a file. What am I doing wrong here

 xwriter = New XmlTextWriter(filename,Encoding.UTF8)
 Dim body As String = System.Text.Encoding.UTF8.GetChars(result.Body)
'body = replaceIllegalXMLChars(body)  ///tried converting them explicitly but did not work
 xwriter.WriteString(post)
 xwriter.Flush()

Sample output:

   <avataruri>http://a0.twimg.com/profile_images/1651487744/Vman_normal.jpg
   </avataruri>
   <suitable>0</suitable>

Upvotes: 0

Views: 81

Answers (1)

SLaks
SLaks

Reputation: 887415

You're misusing XmlTextWriter.

XmlTextWriter helps you generate your own XML by escaping text and writing tag names.
Calling WriteString() will write the text you pass as XML content, correctly escaping special characters.

If you have a string of existing XML, and you want to write it to a file, you should write it just like any other string, using the File class.

You don't even need to decode the bytes; you can write them directly to disk.

Upvotes: 1

Related Questions