469,651 Members | 1,643 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,651 developers. It's quick & easy.

XmlDocument object to string - plase help with unusual character!

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!

Mar 14 '06 #1
3 1612
Almurph,

Can you try this one on our website, which works forever smooth

http://www.vb-tips.com/default.aspx?...c-61641f5c8d9d

If it is a dataset than you can try this one.

http://www.vb-tips.com/default.aspx?...a-c891846eaf0b

I hope this helps,

Cor
<al*****@altavista.com> schreef in bericht
news:11**********************@i39g2000cwa.googlegr oups.com...
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!

Mar 14 '06 #2

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.

Mar 14 '06 #3

AlanT wrote:
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.

Mar 16 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Martin Honnen | last post: by
2 posts views Thread by Adam Smith | last post: by
1 post views Thread by AJ Brown | last post: by
1 post views Thread by Joe Monnin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.