Home All Groups Group Topic Archive Search About

XmlDocument object to string - plase help with unusual character!

Author
14 Mar 2006 4:29 PM
almurph@altavista.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!

Author
14 Mar 2006 4:44 PM
Cor Ligthert [MVP]
Almurph,

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

http://www.vb-tips.com/default.aspx?ID=7ffd296f-9e81-47e6-88dc-61641f5c8d9d

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

http://www.vb-tips.com/default.aspx?ID=06d9730e-9e33-404c-947a-c891846eaf0b

I hope this helps,

Cor


<almu***@altavista.com> schreef in bericht
Show quoteHide quote
news:1142353764.074431.194810@i39g2000cwa.googlegroups.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!
>
Author
14 Mar 2006 5:59 PM
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.
Author
16 Mar 2006 10:34 AM
almurph@altavista.com
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.