By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,251 Members | 2,760 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,251 IT Pros & Developers. It's quick & easy.

Odd character returned when using Encoding.UTF8.GetString(MemoryStream)

P: n/a
Is anyone aware why the following code (intended to write XML into a
memory-based XmlTextWriter, and return the complete document as a string)
produces badly formed XML due to the resultant string always commencing with
a question mark

string xmlRequestString;
MemoryStream memoryStream = new MemoryStream();
XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream,
Encoding.UTF8);

xmlTextWriter.WriteStartDocument();
xmlTextWriter.WriteStartElement("element");
xmlTextWriter.WriteStartElement("subelement");
xmlTextWriter.WriteAttributeString("attribute", "attributeValue");
xmlTextWriter.WriteString("string");
xmlTextWriter.WriteEndElement();
xmlTextWriter.WriteEndElement();
xmlTextWriter.WriteEndDocument();
xmlTextWriter.Close();

xmlRequestString = Encoding.UTF8.GetString(memoryStream.ToArray());
Console.WriteLine(xmlRequestString);
This returns the following (note the preceding question mark):

?<?xml version="1.0" encoding="utf-8"?><element><subelement
attribute="attribute
Value">string</subelement></element>

Any ideas or assistance very gratefully received!!

Many thanks,

Chris.
Jul 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Chris Lacey <ch*********@bigfoot.com> wrote:
Is anyone aware why the following code (intended to write XML into a
memory-based XmlTextWriter, and return the complete document as a string)
produces badly formed XML due to the resultant string always commencing with
a question mark


<snip>

It's a byte ordering mark. If instead of using Encoding.UTF8.GetString
you use a StreamReader, you'll see it goes away.

If you really only want the characters, I suggest that you change to
using a StringWriter instead of a MemoryStream. To support appropriate
encodings, you may wish to derive a class from StringWriter to allow
you to specify the encoding, e.g.

public class StringWriterWithEncoding : StringWriter
{
Encoding encoding;

public StringWriterWithEncoding (Encoding encoding)
{
this.encoding = encoding;
}

public override Encoding Encoding
{
get { return encoding; }
}
}

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jul 21 '05 #2

P: n/a
Chris Lacey wrote:
Is anyone aware why the following code (intended to write XML into a
memory-based XmlTextWriter, and return the complete document as a string)
produces badly formed XML due to the resultant string always commencing with
a question mark


Most likely it's not question mark, but BOM - byte order mark.
Btw, why don't you write directly to string using StringWriter?
--
Oleg Tkachenko
XML Insider
http://www.tkachenko.com/blog

Jul 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.