Results 1 to 4 of 4

Thread: [RESOLVED] XmlWriter writes extra character!!!

  1. #1

    Thread Starter
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Resolved [RESOLVED] XmlWriter writes extra character!!!

    Hello all,
    For the life of me, I can't figure out why the xmlwirter writes an extra "?" at the begining of the xml.
    Hope one of you can spot what I've wrong...
    Below is the code to demonstrate that behavior.
    Code:
     Using ms As New MemoryStream()
                Dim settings As New XmlWriterSettings()
                With settings
                    .CloseOutput = False
                    .CheckCharacters = True
                    .OmitXmlDeclaration = False
                    .Encoding = Encoding.GetEncoding("UTF-8")
                    .Indent = True
                    .IndentChars = "    "
                    .ConformanceLevel = ConformanceLevel.Document
                End With
    
                Dim writer As XmlWriter = XmlWriter.Create(ms, settings)
                With writer
                    ms.Position = 0
                    .WriteStartDocument()
                    .WriteStartElement("AccessRequest")
                    .WriteAttributeString("xml", "lang", Nothing, "en-US")
                    .WriteElementString("AccessLicenseNumber", "12345")
                    .WriteElementString("UserId", "my user id")
                    .WriteElementString("Password", "my password")
                    .WriteEndElement()
                End With
                writer.Flush()
                writer.Close()
                ms.Position = 0
    
                Dim size As Integer = CInt(ms.Length)
                Dim buffer(size - 1) As Byte
                ms.Read(buffer, 0, size)
                Dim output As String = Encoding.UTF8.GetString(buffer)
                Console.Write(output)
            End Using
    And this is the output. Note the red "?" at the beginning of the xml. Upon close examination, it's a Chr(239) but in console application, it is shown as the "?"
    Code:
    ?<?xml version="1.0" encoding="utf-8"?>
    <AccessRequest xml:lang="en-US">
        <AccessLicenseNumber>12345</AccessLicenseNumber>
        <UserId>my user id</UserId>
        <Password>my password</Password>
    </AccessRequest>
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  2. #2
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: XmlWriter writes extra character!!!

    Is that the Byte Order Mark?

    http://en.wikipedia.org/wiki/Byte-order_mark

  3. #3

    Thread Starter
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: XmlWriter writes extra character!!!

    Quote Originally Posted by Negative0 View Post
    Thank you. I bet it is... And thanks to what you pointed out, I modify the code to return the xml in a byte array instead of a string. This is event better because I have to send that xml over the the internet, so if I use an xml string, I will have to convert it back to a byte array before sending it... By returning a byte array, I get rid of the byte order mark problem (although I was able to workaround that by using string.substring to remove the 1st character) and reduce 2 unecessary conversions.

    It's resolved! Thanks again Neg.

    Edit: I must spread some rep around first before I can rep+ you again
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  4. #4
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: [RESOLVED] XmlWriter writes extra character!!!

    Oh, and if you need to absolutely get rid of it, you can use this:

    Code:
     Using ms As New MemoryStream()
                Dim utf8encoder As New UTF8Encoding(False)  'Note, the false is redundant, because it is default, but it demonstrates the point
                Dim settings As New XmlWriterSettings()
                With settings
                    .CloseOutput = False
                    .CheckCharacters = True
                    .OmitXmlDeclaration = False
    
                    .Encoding = utf8encoder
                    .Indent = True
                    .IndentChars = "    "
                    .ConformanceLevel = ConformanceLevel.Document
    
                End With
    
                Dim writer As XmlWriter = XmlWriter.Create(ms, settings)
                With writer
                    ms.Position = 0
                    .WriteStartDocument()
                    .WriteStartElement("AccessRequest")
                    .WriteAttributeString("xml", "lang", Nothing, "en-US")
                    .WriteElementString("AccessLicenseNumber", "12345")
                    .WriteElementString("UserId", "my user id")
                    .WriteElementString("Password", "my password")
                    .WriteEndElement()
                End With
                writer.Flush()
                writer.Close()
                ms.Position = 0
    
                Dim size As Integer = CInt(ms.Length)
                Dim buffer(size - 1) As Byte
                ms.Read(buffer, 0, size)
                Dim output As String = Encoding.UTF8.GetString(buffer)
    
    
                MessageBox.Show(output)
            End Using

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width