HighTechTalks DotNet Forums  

MemoryStream, Backslash-Zero Problem

Dotnet XML microsoft.public.dotnet.xml


Discuss MemoryStream, Backslash-Zero Problem in the Dotnet XML forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
rasx
 
Posts: n/a

Default MemoryStream, Backslash-Zero Problem - 03-09-2006 , 08:29 PM






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

Reply With Quote
  #2  
Old   
dickster
 
Posts: n/a

Default Re: MemoryStream, Backslash-Zero Problem - 03-10-2006 , 03:21 AM






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


Reply With Quote
  #3  
Old   
rasx
 
Posts: n/a

Default Re: MemoryStream, Backslash-Zero Problem - 03-12-2006 , 10:07 PM



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:

Quote:
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



Reply With Quote
  #4  
Old   
Amol Kher [MSFT]
 
Posts: n/a

Default Re: MemoryStream, Backslash-Zero Problem - 03-31-2006 , 03:21 AM



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

Quote:
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





Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.