Microsoft KB Archive/888226

= You receive an unhandled exception error message when you use the MemoryStream class in the .NET Framework 1.1 Service Pack 1 =

Article ID: 888226

Article Last Modified on 12/3/2007

-

APPLIES TO


 * Microsoft .NET Framework 1.1 Service Pack 1

-





SYMPTOMS
When you try to use the MemoryStream class to read a memory stream in a Microsoft Visual C# .NET 2003 project, you may receive an error message that is similar to one of the following:

Unhandled Exception: System.Xml.XmlException: The root element is missing.

Unhandled Exception: System.IO.SomeStreamException: The memory stream is at the end of the buffer.

Unhandled Exception: System.IO.SomeReaderException: The reader cannot read anymore.



CAUSE
This behavior occurs if the following conditions are true:
 * You install the Microsoft .NET Framework 1.1 Service Pack 1 (SP1).
 * The memory stream is positioned at the end-of-stream.



WORKAROUND
To work around this behavior, reset the stream position before you create the XmlTextReader class. For an example, see the &quot;More Information&quot; section.



Steps to reproduce the behavior
  Create a text file that is named Output.txt, and then paste the following code in Output.txt:   SUCCESS:test </li> <li>In Visual C# .NET, create a new Console Application project. By default, Class1.cs is created.</li> <li> Paste the following code in the Class1.cs file: using System; using System.IO; using System.Text; using System.Xml;

public class Form1 {   static void Main {       try {

Stream _oldStream = new FileStream(@&quot;Output.txt&quot;, FileMode.Open, FileAccess.Read); Stream _newStream = new MemoryStream;

TextReader tr = new StreamReader(_oldStream); TextWriter tw = new StreamWriter(_newStream); tw.WriteLine(tr.ReadToEnd); tw.Flush;

XmlReader reader = new XmlTextReader( new StreamReader( _newStream ) ); _newStream.Position = 0; _newStream.Seek( 0, SeekOrigin.Begin ); reader.Read;

while( reader.Read );

Console.WriteLine(&quot;Success&quot;); }       catch (Exception ex) {           System.Diagnostics.Debug.WriteLine(ex.ToString); Console.WriteLine(&quot;Exception : &quot; + ex.Message + &quot;\n&quot; + ex.StackTrace); }   } } </li> <li>Compile and then run the application.</li></ol>

Keywords: kbappdev kbprogramming kbtshoot kbprb KB888226

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.