473,387 Members | 1,379 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,387 software developers and data experts.

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

Similar topics

3
by: Nicolas | last post by:
Hi Everybody, I'm working with the MemoryStream and I'm having problems with the ReadBytes method. When I use it, this method never returns bytes!!!! MemoryStream sw = new MemoryStream();...
2
by: SQLScott | last post by:
For some reason I am drawing a blank on this, so I would be extremely greatful if someone could help me out. I am trying to get a MemoryStream out of a Byte array. How do I get it back out? ...
13
by: Don | last post by:
When I run the following code, the MemoryStream's Position is always set to 762 instead of 0, which is what I would expect: Dim bmp As Image Dim ms As MemoryStream bmp = New...
2
by: John Dann | last post by:
I guess there must be some convention or Windows specification for whether the backslash immediately preceding the file name in a full path string to a file is formally part of the path string or...
3
by: Stef Mientki | last post by:
It looks like sometimes a single backslash is replaced by a double backslash, but sometimes it's not ??? See the error message below, the first backslash is somewhere (not explicitly in my code)...
4
by: | last post by:
Hi all, I want to create a method that does the following: 1) Programmatically instantiate a new XmlDataSource control 2) For each file in a named directory, make a "FileSystemItem" element 3)...
3
by: =?Utf-8?B?UGhpbCBKb2huc29u?= | last post by:
Hi, I am using dotnet remoting with a binarry formatter. I have a property that returns a memorystream that has had a file loaded into it. When I try to access this property though I get an...
2
by: Tobiah | last post by:
>>"'" "'" "'" "\\'" "\\'" This is quite different than any other language that I am used to. Normally, a double backslash takes away the special meaning of the last backslash, and so you...
5
by: vlsidesign | last post by:
The printf function returns "warning: unknown escape sequence: \040" for a backslash-space combination. If the ascii decimal number for space is 32 and the backslash is 92, why this particular...
4
by: Razzbar | last post by:
I'm working on a bookmarklet that grabs information from a page and submits it to a server. Yet another social bookmarking application. I'm having trouble with page titles that include an...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.