Friday, July 23, 2004 12:26 AM bart

StackOverflowException during XML Serialization

Developers who've been using the XML Serialization classes in the .NET Framework will probably recognize the "System.InvalidOperationException" exception that is thrown whenever is problem occurs during the deserialization. In fact, the used code for deserizalization is as easy as this:

XmlSerializer ser = new XmlSerializer(typeof(TargetClass));
return (TargetClass) ser.Deserialize(stream);

However, whenever a problem occurs, the same kind of Exception is thrown with the cool message: "There is an error in XML document (x,y)" with x and y replaced by the row and column in the XML source where the problem occurs. The first trick you should know is to look at the InnerException property of the exception to find out what the real problem was. In fact, I just did something like this:

  • Serialize the class to XML as follows:
    XmlSerializer ser = new XmlSerializer(typeof(TargetClass));
    ser.Serialize(stream,
    this);
    stream.Close();
  • Deserialize again (other method):
    XmlSerializer ser = new XmlSerializer(typeof(TargetClass));
    return (TargetClass) ser.Deserialize(stream);

However, this still resulted in an exception :-(. The inner exception was as nice as the cryptic wrapper "InvalidOperationException": StackOverflowException. It took me quite some time to find out where the problem was. XML serialization uses the properties of the class's instance to make these persistent in the XML target stream. To make sure everything is working correctly, I needed to expose some attributes as properties to make these persistent (in fact, I only had some indirect methods that were setting the attributes, so these were not made persistent since there was no equivalent property for the attributes). Assume the attribute was:

private SomeOtherClass attribute;

and the corresponding property was:

public SomeOtherClass Attribute
{
   get
   {
      return attribute;
   }
   set
   {
      attribute =
value;
   }
}

Guess where the error was? Yeah, inside the setter of the method I made a mistake:

      Attribute = value;

Classic story, property calls its own to set the value, over and over again... However, not easy to find if the exception is wrapped up inside some other exception which is thrown during the deserialization process. In fact, the setter was only there to make the XML serialization possible, so it was not tested upfront (shame on you, Bart :-)).

Now I'm happy. Not because the exceptions are gone, but because I end up with another one. Okay, it's the "InvalidOperationException" again but this time the InnerException is "The data at the root level is invalid". Ready for the next debugging burst...

The moral of the story: be happy with exceptions always :-)

Del.icio.us | Digg It | Technorati | Blinklist | Furl | reddit | DotNetKicks

Filed under:

Comments

# re: StackOverflowException during XML Serialization

Saturday, July 24, 2004 7:17 AM by bart

Welcome back to your blog ;)
I´ve missed you..

# re: StackOverflowException during XML Serialization

Saturday, July 24, 2004 11:23 PM by bart

I've been off for a short holiday.

# re: StackOverflowException during XML Serialization

Saturday, January 08, 2005 2:44 AM by bart

You helped me out! Thanks. Same problem, but I was seeing it on Serialize, not Deserialize. Why? Because my Getter was causing the loop, not the setter. Yucky. The reason I had the set/get in the first place was to make the public property not serialize. I guess I'll have to find another way. :-)