[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>
Re: XmlWriter writes extra character!!!
Re: XmlWriter writes extra character!!!
Quote:
Originally Posted by
Negative0
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 :)
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