468,268 Members | 1,754 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,268 developers. It's quick & easy.

MemoryStream, Backslash-Zero Problem

I get this error when I fail to ‘clean up’ my UTF-8 MemoryStream: “Invalid at
the top level of the document. Error processing resource…” Visual Studio 2005
in debug mode shows well formed XML “prepended” with a character represented
by an empty rectangle and trailed with a series of backslash zeros (\0).
These are the lines of code that solve this problem:

s = s.Trim();
s = s.Replace("\0", String.Empty);

The Trim() gets rid of the “prepended” mystery character and the second line
explains itself. Why do I need these two line of code? Here is more context:

MemoryStream ms = new MemoryStream();
XmlTextWriter xw = new XmlTextWriter(ms,Encoding.UTF8);

//do XMLWriter stuff…

xw.Flush();
s = Encoding.UTF8.GetString(ms.GetBuffer());

s = s.Trim();
s = s.Replace("\0", String.Empty);

xml = new XmlDocument();
xml.LoadXml(s);

By the way, I get the same error when keep everything in the MemoryStream
and make the statement xml.Load(ms). I see no “stream visualizer” for
debugging purposes.

--
Bryan, Emperor of String.Empty
http://songhaysystem.com
Mar 10 '06 #1
3 2435
I have seen that empty rectangle before- seems like default behaviour
-why I have no idea.

There is a parameter "encoderShouldEmitUTF8Identifier" (type Boolean)
as one of the options on the constructor of the
System.Text.UTF8Encoding
So add these 2 lines
--------------------------------------------------------
System.text.UTF8Encoding enc;
enc = New System.text.UTF8Encoding(False);
--------------------------------------------------------

And change your code to this
--------------------------------------------------------
MemoryStream ms = new MemoryStream();
XmlTextWriter xw = new XmlTextWriter(ms,enc);
--------------------------------------------------------

Dickster

Mar 10 '06 #2
The short answer to the earlier post is, ‘Don’t use a MemoryStream object
with an XmlWriter.’ The expected results are received in my case when an
HttpContext.Response.OutputStream is used instead.
--
Bryan, Emperor of String.Empty
http://songhaysystem.com
"dickster" wrote:
I have seen that empty rectangle before- seems like default behaviour
-why I have no idea.

There is a parameter "encoderShouldEmitUTF8Identifier" (type Boolean)
as one of the options on the constructor of the
System.Text.UTF8Encoding
So add these 2 lines
--------------------------------------------------------
System.text.UTF8Encoding enc;
enc = New System.text.UTF8Encoding(False);
--------------------------------------------------------

And change your code to this
--------------------------------------------------------
MemoryStream ms = new MemoryStream();
XmlTextWriter xw = new XmlTextWriter(ms,enc);
--------------------------------------------------------

Dickster

Mar 13 '06 #3
I think the issue you are seeing is because you dont set the MemoryStream
back to its initial position before passing it into the Dom?

ms.Seek( 0, SeekOrigin.Begin );
"rasx" <rasx (an email account) at songhaysystem.com> wrote in message
news:FE**********************************@microsof t.com...
The short answer to the earlier post is, 'Don't use a MemoryStream object
with an XmlWriter.' The expected results are received in my case when an
HttpContext.Response.OutputStream is used instead.
--
Bryan, Emperor of String.Empty
http://songhaysystem.com
"dickster" wrote:
I have seen that empty rectangle before- seems like default behaviour
-why I have no idea.

There is a parameter "encoderShouldEmitUTF8Identifier" (type Boolean)
as one of the options on the constructor of the
System.Text.UTF8Encoding
So add these 2 lines
--------------------------------------------------------
System.text.UTF8Encoding enc;
enc = New System.text.UTF8Encoding(False);
--------------------------------------------------------

And change your code to this
--------------------------------------------------------
MemoryStream ms = new MemoryStream();
XmlTextWriter xw = new XmlTextWriter(ms,enc);
--------------------------------------------------------

Dickster

Mar 31 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Nicolas | last post: by
13 posts views Thread by Don | last post: by
3 posts views Thread by Stef Mientki | last post: by
3 posts views Thread by =?Utf-8?B?UGhpbCBKb2huc29u?= | last post: by
2 posts views Thread by Tobiah | last post: by
4 posts views Thread by Razzbar | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.