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