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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris Lacey

    #1

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

    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 xmlRequestStrin g;
    MemoryStream memoryStream = new MemoryStream();
    XmlTextWriter xmlTextWriter = new XmlTextWriter(m emoryStream,
    Encoding.UTF8);

    xmlTextWriter.W riteStartDocume nt();
    xmlTextWriter.W riteStartElemen t("element");
    xmlTextWriter.W riteStartElemen t("subelement") ;
    xmlTextWriter.W riteAttributeSt ring("attribute ", "attributeValue ");
    xmlTextWriter.W riteString("str ing");
    xmlTextWriter.W riteEndElement( );
    xmlTextWriter.W riteEndElement( );
    xmlTextWriter.W riteEndDocument ();
    xmlTextWriter.C lose();

    xmlRequestStrin g = Encoding.UTF8.G etString(memory Stream.ToArray( ));
    Console.WriteLi ne(xmlRequestSt ring);


    This returns the following (note the preceding question mark):

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

    Any ideas or assistance very gratefully received!!

    Many thanks,

    Chris.


  • Jon Skeet [C# MVP]

    #2
    Re: Odd character returned when using Encoding.UTF8.G etString(Memory Stream)

    Chris Lacey <chris.lacey@bi gfoot.com> wrote:[color=blue]
    > 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[/color]

    <snip>

    It's a byte ordering mark. If instead of using Encoding.UTF8.G etString
    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 StringWriterWit hEncoding : StringWriter
    {
    Encoding encoding;

    public StringWriterWit hEncoding (Encoding encoding)
    {
    this.encoding = encoding;
    }

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

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Oleg Tkachenko

      #3
      Re: Odd character returned when using Encoding.UTF8.G etString(Memory Stream)

      Chris Lacey wrote:
      [color=blue]
      > 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[/color]

      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


      Comment

      Working...