Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I've got to edit an XSLT stylesheet, but I'm flying blind because the XML input only exists fleetingly in a bunch of streams. I can debug into the code, but can't figure out how to get the contents of the streams out into text I can look at (and run through the XSLT manually while I'm editing them).

The code is part of a big old legacy system, I can modify it in a debug environment if absolutely necessary, but it runs in a windows service connected up to a bunch of MSMQs. So for various reasons I'd rather be able to use the debugger to see the XML without having to change the code first.

Code much simplified, is something like this: (C# - but remember it's .net 1.1 in VS 2003.)

This is the function that gets the XML as a stream, which is then fed into some sort of XSLT transform object. I've tried looking at the writer and xmlStream objects in the watch windows and the immediate window, but can't quite fathom how to see the actual XML.

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;
    return xmlStream; //Goes off to XSLT transform thingy!
}

All help much appreciated.

like image 788
Andrew M Avatar asked May 06 '09 16:05

Andrew M


People also ask

How do I view an XML file in Visual Studio?

In the document properties window, click the browse button (...) on the Schemas field. The XSD Schemas dialog box is displayed. Click Add. The Open XSD Schema dialog box is displayed.

How do I Debug XML in Visual Studio?

Debug from the XML editor You can start the debugger when you have either a style sheet or an input XML file open in the editor. This lets you debug as you're designing the style sheet. Open the style sheet or XML file in Visual Studio. Select Start XSLT Debugging from the XML menu or press Alt+F5.


2 Answers

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.

like image 51
PeterAllenWebb Avatar answered Nov 03 '22 08:11

PeterAllenWebb


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.

like image 34
Andrew M Avatar answered Nov 03 '22 09:11

Andrew M