472,805 Members | 1,593 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,805 software developers and data experts.

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 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
2 2394
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: xmlguy | last post by:
PREVIOUS BACKGROUND POST: I am trying to reuse a Memory Stream for loading and transforming the xml it contains Basically I have defined following interfaces: Class Render {
2
by: Chris Lacey | last post by:
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...
10
by: Asaf | last post by:
Hi, I am trying to Compress & Decompress a DataSet. When running this code I am receiving the error: System.Xml.XmlException was unhandled Message="Root element is missing."...
3
by: JDeats | last post by:
I have some .NET 1.1 code that utilizes this technique for encrypting and decrypting a file. http://support.microsoft.com/kb/307010 In .NET 2.0 this approach is not fully supported (a .NET 2.0...
3
by: melon | last post by:
I need to store some password on a text file. I was trying to use some kind of encryption to encrypt the password from plain text. I found the code below off the web, which works great. But...
1
by: SuperKhoolGuy | last post by:
Guys, I have a client tool which runs and accepts files with "amt" extensions. All the "amt" files have been setup to open using my client tool which works great when I double-click on the...
10
by: Atara | last post by:
Suppose I have the following functions: Public Function myF1ToStream(...) As System.IO.MemoryStream . . . Dim ms As New System.IO.MemoryStream(buf) Return ms End Function Public Function...
7
by: Andrew | last post by:
Hi, I am using DataContractJsonSerializer to deserialize JSON string in C# objects but I am having a problem. Suppose I have a class: class Item { public ItemId Id { get; set; }
4
by: =?Utf-8?B?Q3JhaWdo?= | last post by:
We are having a problem whenever our application makes use of the XML Serializer, when we are logged in as a user who has a username containing Japanese characters. We have prepared a sample...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.