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. I am 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 ORCS Web
Saturday, November 06, 2004 10:56:53 AM UTC
I like the font you use in VS.NET. What is it called?
Hermann Klinke
Sunday, November 07, 2004 12:52:57 AM UTC
That's "ProFont" - the Programmer's Font

http://www.tobias-jung.de/seekingprofont/
Monday, November 08, 2004 4:36:18 PM UTC
Wierd coincidence, I helped someone debug a related problem last week; it turns out that this trick comes in handy if you generate a proxy for a WSDL that has a bad schema. This will manifest itself with a System.IO.FileNotFound exception when you hit the asmx page, what it's telling you is that the serializer tried to generate code for the schema but failed. KB article 823196 documents all this.
Comments are closed.

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