How to inspect XML streams from the debugger in Visual Studio 2003 How to inspect XML streams from the debugger in Visual Studio 2003 xml xml

How to inspect XML streams from the debugger in Visual Studio 2003


You could simply add this expression to your watch window after the MemoryStream is ready:

(new StreamReader(xmlStream)).ReadToEnd();

Watch expressions don't need to be simple variable values. They can be complex expressions, but they will have side-effects. As you've noted, this will interrupt execution, since the stream contents will be read out completely. You could recreate the stream after the interruption with another expression, if you need to re-start execution.

This situation arises frequently when debuging code with streams, so I avoid them for simple, self-contained tasks. Unfortunately, for large systems, it's not always easy to know in advance whether you should make your code stream-oriented or not, since it depends greatly on how it will be used. I consider the use of streams to be a premature optimization in many cases, however.


OK, I've not succeeded in using the debugger without modifying the code. I added in the following snippet, which lets me either put a breakpoint in or use debugview.

private MemoryStream GetXml(){    MemoryStream xmlStream;    xmlStream = new MemoryStream();    XmlWriter writer = new XmlTextWriter(xmlStream, Encoding.UTF8);    writer.WriteStartDocument();    //etc etc...    writer.WriteEndDocument();    writer.Flush();    xmlStream.Position = 0;    #if DEBUG    string temp;    StreamReader st=new StreamReader(xmlStream);    temp=st.ReadToEnd();    Debug.WriteLine(temp);    #endif    return xmlStream; //Goes off to XSLT transform thingy!}

I'd still prefer to simply look at the xmlstream object in the debugger somehow, even if it disrupts the flow of execution, but in the meantime this is the best I've managed.