XmlDocument object to string - plase help with unusual character!

A

almurph

Hi everyone,

Hope you can help me. I'm converting an XMlDocuemnt object to a
string. I use the following code:


Dim doc As New XmlDocument

'Convert XmlDocument object -> String
Dim ms As New System.IO.MemoryStream
Dim xtw As XmlTextWriter = New XmlTextWriter(ms, Encoding.UTF8)
doc.Save(xtw)
Dim bytes() As Byte = ms.ToArray()
Dim st As String = Encoding.UTF8.GetString(bytes)


Only problem is though - when I debug the code it inserts a little
unusual character at the start of the xml string. It looks like a
little box in the "yellow" window and when I print it it looks lioke a
little dot. This strange character is making hte XML invalid.

Anyone with any ideas/suggestions/comments/code-snipets that can help.
Would greatly appreciate any assistance - I'm at my wits end.

Al.
The confused!
 
A

AlanT

The character is the Byte Order Mark (BOM) - don't have a reference to
hand but a web search will give you more information if interested

To prevent the writer emitting it you can use

Dim encoding As New System.Text.UTF8Encoding '
the default false arg for the ctor suppresses the BOM
Dim xtw As XmlTextWriter = New XmlTextWriter(ms, encoding)

.....

Dim st as string = encoding.GetString(bytes)


hth,
Alan.
 
A

almurph

AlanT said:
The character is the Byte Order Mark (BOM) - don't have a reference to
hand but a web search will give you more information if interested

To prevent the writer emitting it you can use

Dim encoding As New System.Text.UTF8Encoding '
the default false arg for the ctor suppresses the BOM
Dim xtw As XmlTextWriter = New XmlTextWriter(ms, encoding)

....

Dim st as string = encoding.GetString(bytes)

Alan,

Thanks a million - I never heard of the BOM before. I love it when I
learn something new. Thank you both for your comments.

Merci,
Al.
The happy one.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top