473,804 Members | 2,124 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(m s,Encoding.UTF8 );

//do XMLWriter stuff…

xw.Flush();
s = Encoding.UTF8.G etString(ms.Get Buffer());

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 2723
I have seen that empty rectangle before- seems like default behaviour
-why I have no idea.

There is a parameter "encoderShouldE mitUTF8Identifi er" (type Boolean)
as one of the options on the constructor of the
System.Text.UTF 8Encoding
So add these 2 lines
--------------------------------------------------------
System.text.UTF 8Encoding enc;
enc = New System.text.UTF 8Encoding(False );
--------------------------------------------------------

And change your code to this
--------------------------------------------------------
MemoryStream ms = new MemoryStream();
XmlTextWriter xw = new XmlTextWriter(m s,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.Res ponse.OutputStr eam 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 "encoderShouldE mitUTF8Identifi er" (type Boolean)
as one of the options on the constructor of the
System.Text.UTF 8Encoding
So add these 2 lines
--------------------------------------------------------
System.text.UTF 8Encoding enc;
enc = New System.text.UTF 8Encoding(False );
--------------------------------------------------------

And change your code to this
--------------------------------------------------------
MemoryStream ms = new MemoryStream();
XmlTextWriter xw = new XmlTextWriter(m s,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.Begi n );
"rasx" <rasx (an email account) at songhaysystem.c om> wrote in message
news:FE******** *************** ***********@mic rosoft.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.Res ponse.OutputStr eam 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 "encoderShouldE mitUTF8Identifi er" (type Boolean)
as one of the options on the constructor of the
System.Text.UTF 8Encoding
So add these 2 lines
--------------------------------------------------------
System.text.UTF 8Encoding enc;
enc = New System.text.UTF 8Encoding(False );
--------------------------------------------------------

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

Dickster

Mar 31 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
10954
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(); sw.Write(System.Text.UnicodeEncoding.Unicode.GetBytes("Testing"),0,6); System.Text.StringBuilder sb = new System.Text.StringBuilder(); byte buffer = new byte; byte result = sw.GetBuffer(); int a = sw.Read(buffer,2,4);
2
2136
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? I have a sub that calls a function: Dim bByte() As Byte bByte = ws.SQLGetUserDemographics()
13
2854
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 System.Drawing.Bitmap("C:\2068.bmp") ms = New MemoryStream bmp.Save(ms, bmp.RawFormat)
2
3249
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 of the file name. I suppose the options are: 1. Part of the path string, ie all returned path strings should have a trailing backslash. 2. Part of the file name, ie all file names should start with a backslash.
3
23022
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) replaced, but the second is not ??? Is it in general better to use double backslash in filepaths ? thanks, Stef Mientki
4
5672
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) On each FileSystemItem Element, make two child nodes, one with the file name, one with the file size. ie. <filesystemitems> <filesystemitem>
3
5566
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 error regarding "the proxy has no channel sink.......or no suitable Client channel to talk to the server."
2
2188
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 are left with a single backslash.
5
15696
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 number 040? Is it a decimal number from the ASCII code chart? (compiling using gcc on SunOS 5.8, Sparc, Ultra-80) Just curious. Thanks.
4
2279
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 apostrophe. I'm using encodeURIComponent() around the page title, and again around the URL. Apparently the browser is inserting a backslash before any apostrophe. I can see that when I write the $_GET data to a file in PHP on the server. When the GET...
0
10600
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10354
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10097
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9175
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 projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6867
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5535
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4313
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3835
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3002
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.