casterle
casterle

Reputation: 1047

VS2010 save array/collection data to a file while debugging

Is there some way to save array/list/collection data to a file while debugging in VS2010?

For example, in this code:

var addressGraphs = from a in context.Addresses
                    where a.CountryRegion == "Canada"
                    select new { a, a.Contact };

foreach(var ag in addressGraphs) {
   Console.WriteLine("LastName: {0}, Addresses: {1}", ag.Contact.LastName.Trim(),
                     ag.Contact.Addresses.Count());



   foreach(var Address in ag.Contact.Addresses) {
      Console.WriteLine("...{0} {1}", Address.Street1, Address.City);
   }
}

I'd like to set a breakpoint on the first 'foreach' line and then save the data in 'addressGraph' to a file.

where 'a' contains fields such as:

   int addressID
   string Street1
   string City
   <Ect.>

and 'Contact' contains fields such as:

   string FirstName
   string LastName
   int contactID
   <Ect.>

I'd like the file to contain the values of each of the fields for each item in the collection.

I don't see an obvious way to do this. Is it possible?

Upvotes: 20

Views: 12424

Answers (7)

Omer Raviv
Omer Raviv

Reputation: 11827

You can also call methods in the Immediate Window, and so I think your best bet would be to use an ObjectDumper object, like the one in the LINQ samples or this one, and then write something like this in the Immediate Window:

File.WriteAllText("myFileName.txt", ObjectDumper.Dump(addressGraph));

Depending on which ObjectDumper you decide to use, you may be able to customize it to suit your needs, and to be able to tell it how many levels deep you want it to dig into your object when it's dumping it.

Upvotes: 1

Traummaennlein
Traummaennlein

Reputation: 486

In "Immediate Window" print following to get the binary dump:

byte[] myArray = { 02,01,81,00,05,F6,05,02,01,01,00,BA };

myArray
  .Select(b => string.Format("{0:X2}", b))
  .Aggregate((s1, s2) => s1 + s2)

This will print something like:

0201810005F60502010100BA

Change the '.Aggregate(...)' call to add blanks between bytes, or what ever you like.

Upvotes: 0

Anton Bakulev
Anton Bakulev

Reputation: 306

I also encoutered such a question, but in VS2013. I have to save a content of array while debugging.

For example, I need to save a content of double array named "trimmedInput". I do so:

  1. Open QuickWatch Window from Debug menu (Ctrl+D, Q). enter image description here

  2. Put your variable in Expression and push Recalculate Button enter image description here

  3. You'll see all the values. Now you could select them all (Ctrl+A) and copy (Ctrl+C).

  4. Paste (Ctrl+V) them in your favorite editor. Notepad, for example. And use them. enter image description here

That's the simples way that I know. Without additional efforts. Hope that my description helps you!

P.S. Sorry for non English interface on screenshots. All necessary information are written in the text.

Upvotes: 8

Ehsan Abidi
Ehsan Abidi

Reputation: 959

On Visual studio Gallery search for: Object Exporter Extension.
be aware: as far as I worked with, it has a bug that block you from exporting object once in a while.

Upvotes: 1

casterle
casterle

Reputation: 1047

Here's a solution that takes care of collections. It's a VS visualizer that will display the collection values in a grid while debugging as well as save to the clipboard and csv, xml and text files. I'm using it in VS2010 Ultimate. While I haven't tested it extensively, I have tried it on List and Dictionary.

http://tinyurl.com/87sf6l7

It handles the following collections:

•System.Collections classes  
   ◦System.Collections.ArrayList  
   ◦System.Collections.BitArray  
   ◦System.Collections.HashTable  
   ◦System.Collections.Queue  
   ◦System.Collections.SortedList  
   ◦System.Collections.Stack  
   ◦All classes derived from System.Collections.CollectionBase  

•System.Collections.Specialized classes  
   ◦System.Collections.Specialized.HybridDictionary  
   ◦System.Collections.Specialized.ListDictionary  
   ◦System.Collections.Specialized.NameValueCollection  
   ◦System.Collections.Specialized.OrderedDictionary  
   ◦System.Collections.Specialized.StringCollection  
   ◦System.Collections.Specialized.StringDictionary  
   ◦All classes derived from System.Collections.Specialized.NameObjectCollectionBase  

•System.Collections.Generic classes  
   ◦System.Collections.Generic.Dictionary
   ◦System.Collections.Generic.List  
   ◦System.Collections.Generic.LinkedList  
   ◦System.Collections.Generic.Queue  
   ◦System.Collections.Generic.SortedDictionary  
   ◦System.Collections.Generic.SortedList  
   ◦System.Collections.Generic.Stack  

•IIS classes, as used by  
   ◦System.Web.HttpRequest.Cookies  
   ◦System.Web.HttpRequest.Files  
   ◦System.Web.HttpRequest.Form  
   ◦System.Web.HttpRequest.Headers  
   ◦System.Web.HttpRequest.Params  
   ◦System.Web.HttpRequest.QueryString  
   ◦System.Web.HttpRequest.ServerVariables  
   ◦System.Web.HttpResponse.Cookies  

As well as a couple of VB6-compatible collections

Upvotes: 0

Durden81
Durden81

Reputation: 1024

Something similar is possible with this method:

I built an extension method that I use in all of my projects that is a general and more powerful ToString() method that shows the content of any object. I included the source code in this link: https://rapidshare.com/files/1791655092/FormatExtensions.cs

UPDATE: You just have to put FormatExtensions.cs in your project and change the Namespace of FormatExtensions to coincide to the base Namespace of your project. So when you are in your breakpoint you can type in your watch window: myCustomCollection.ToStringExtended()

And copy the output wherever you want

Upvotes: 2

Mrchief
Mrchief

Reputation: 76218

When your breakpoint is hit, open up the Immediate window and use Tools.LogCommandWindowOutput to dump the output to a file:

>Tools.LogCommandWindowOutput c:\temp\temp.log
?addressGraphs
>Tools.LogCommandWindowOutput /off

Note: You can use Log which is an alias for Tools.LogCommandWindowOutput


Update: The > character is important. Also, the log alias is case sensitive. See screenshot:

enter image description here

Upvotes: 30

Related Questions