Debugging large datasets
You call a webservice and get back a datastructure that is ~1 mb. How do you in a debug session make sure that it contains the data you expect?
I'm not really interested in the name BRODERIC in my code, but I would like to know if it is there.
I could write a test that will tell me, but I might also want a faster and more temporary way to get this knowledge.
In your Visual Studio you have an Immediate window that will allow you to write code in debug mode. This is very useful. Start a debugging session and open the menu Debug/Windows/Immediate, or press CTRL+ALT+I.
This is a very simple datastructure, but let's pretend it is a complex one and we would like to get it as XML. Start with a xml serializer. I prefer the DataContractSerializer (you need to have a reference to System.Runtime.Serialization in your current project).
System.Runtime.Serialization.DataContractSerializer serializer = new System.Runtime.Serialization.DataContractSerializer(names.GetType());
It takes the type to serialize in it's constructor. This should work even with an anonymous type. Next create the output stream that serialization should be made to.
System.IO.MemoryStream outputStream = new System.IO.MemoryStream();
Now you can serialize the contents of names to this stream.
The immediate window will tell you that the expression has been evaluated, but has no value. That is correct. Now we need to reset the stream if we're to read from it.
outputStream.Position = 0;
Now we're ready to read the xml from that stream and put it into a variable. That variable will then be available in our Locals window.
string outputXml = new System.IO.StreamReader(outputStream).ReadToEnd();
The locals window is also available in the menu Debug/Windows/Locals.
Now you can copy paste the xml to your favorite text editor and search for the value you're looking for. You can communicate the data to your customer and you can prove to Mr Andersson that you certainly don't get the values from his service that you're expecting. Happy coding!