0

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>

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

oh yes...I just copied this from old code to create xml documents but I guess I could just use a streamwriter or stringbuilder and write to a text file if I am understanding you correctly right?
I want to create an xml document and the message is already formatted in xml format.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.