Scott Hanselman

HOW TO: Debug into a .NET XmlSerializer Generated Assembly

November 6, '04 Comments [3] Posted in ASP.NET | XmlSerializer | Bugs
Sponsored By

The XmlSerializer is a much maligned piece of software, but I have to tell you, it's the bomb.  We use it a lot here at Corillian. Recently we had to debug a pretty funky problem where an enum was writing out to an XML file, but wasn't reading back in. We suspected it was a namespace thing, but the XmlSerializer is such a black box, a lot of people really have trouble dealing with it. It inspires a trial-and-error style, while I prefer to debug and step around myself.

Here's how to debug into a generated assembly from the XmlSerializer.

1. Given an application like:

using System.Xml;
using System.Xml.Serialization;
using System.Text;
using System.IO;

namespace Foo
{
    public class Bar
    {
        public static void Main(string[] args)
        {
            XmlSerializer x = new XmlSerializer(typeof(Person));
            Person p = new Person();
            p.first = "Scott";
            p.last = "Hanselman";
            x.Serialize(new StreamWriter("foo.xml"),p);
        }
    }

    public class Person
    {
        public string first;
        public string last;
    }
}

2. Create a yourapplication.exe.config like this:

<?xml version="1.0" encoding="utf-8" ?>
<
configuration>
   <system.diagnostics>
      <switches>
         <add name="XmlSerialization.Compilation" value="1" />
      </switches>
   </system.diagnostics>
</
configuration>

3. Compile and run and step up to the line where the XmlSerializer is constructed, and step over that line.

4. Go to c:\documents and settings\[username]\local settings\temp and look at the most recently created *.cs file. Open that file, it will have a name like asdasdfs.0.cs. Note that there are *.pdbs in that folder as well.

5. Set a breakpoint anywhere in that generated file.

6. Debug to taste (see screenshot below)

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.