Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old March 10th, 2006, 02:45 AM
rasx
Guest
 
Posts: n/a
Default 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
  #2  
Old March 10th, 2006, 09:35 AM
dickster
Guest
 
Posts: n/a
Default Re: MemoryStream, Backslash-Zero Problem

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

  #3  
Old March 13th, 2006, 04:15 AM
rasx
Guest
 
Posts: n/a
Default Re: MemoryStream, Backslash-Zero Problem

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:
[color=blue]
> 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
>
>[/color]
  #4  
Old March 31st, 2006, 08:35 AM
Amol Kher [MSFT]
Guest
 
Posts: n/a
Default Re: MemoryStream, Backslash-Zero Problem

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:FE3CA3EB-9A73-4351-B00D-26FC04469002@microsoft.com...[color=blue]
> 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:
>[color=green]
>> 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
>>
>>[/color][/color]


 

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

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

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles