Scott Hanselman

HOW TO: Debug into a .NET XmlSerializer Generated Assembly

November 06, 2004 Comment on this post [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
Hosting By
Hosted in an Azure App Service
November 06, 2004 14:56
I like the font you use in VS.NET. What is it called?
November 07, 2004 4:52
That's "ProFont" - the Programmer's Font

http://www.tobias-jung.de/seekingprofont/
November 08, 2004 20:36
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.