473,395 Members | 1,616 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 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 2425
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.