doogal.co.uk Home

C# XML Debugger



If you're using XmlTextWriter to generate XML documents, it's pretty easy to generate invalid XML. Unfortunately it's not particularly easy to then find out what is wrong with the XML without saving to a text file and opening it in IE. You can write out a debug string using Debug.Write but that just appears as straight text. Hence I've written this little bit of code that hooks into Debug.Write and chucks the data into the IE ActiveX control in the debug server application.

This shows a few interesting .NET features. Firstly it shows how easy it is to write your own class to hook into Debug.Write. It also shows how to use the UdpClient class. Finally it shows how to do some threading and synchronise the UI via the main thread.

Here's the class you need to reference in your application to be debugged.

This form shows how to register the debugger and has a button to send some XML to it.

Finally, this ZIP file contains the source for the debug server.

And even more finally, thanks to Greg for the original idea and implementation in Delphi using WM_COPYDATA.

Update

The initial implementation kept recreating the UDP connection in the server after every piece of data received. The reason I did this was because when I didn't the server blew up on my machine. But the problem with this is that packets can get lost if the connection is closed when they are sent. So I decided to track down the initial problem. It seems it has something to do with ZoneAlarm. If it isn't running, everything is fine but when it is, after the first message, nothing works. Anyway I've updated my code to work better and if you've got ZoneAlarm be sure to stop it before using the XML debugger...

I've also updated the client-side code so you don't have to hook into Debug.Write if you don't want to.

Another update

It's never easy is it? It seems there is a bit of a problem with UDP, or .NET's implementation of it at least. There's a limit to the size of data you can send through it. I haven't worked out if it's possible to increase the buffer size, so in the mean time I've gone back to my old WM_COPYDATA implementation. So why not use this all the time? Well it doesn't work too well if you try it from a web server (or any kind of service I'd guess). But it does allow you to send as much XML as you want. The server now accepts UDP and WM_COPYDATA and there are two implementations of the client class, so take your pick.

And another update

I don't know if there is a tool out there that already does this but I couldn't find it if there is. I just wanted to quickly check my XPath expressions would return something sensible, so I've added it to the XML debugger.