XmlSerializer - There was an error reflecting type


Using C# .NET 2.0, I have a composite data class that does have the [Serializable] attribute on it. I am creating an XMLSerializer class and passing that into the constructor:

XmlSerializer serializer = new XmlSerializer(typeof(DataClass));

I am getting an exception saying:

There was an error reflecting type.

Inside the data class there is another composite object. Does this also need to have the [Serializable] attribute, or by having it on the top object, does it recursively apply it to all objects inside?

9/19/2013 2:11:33 PM

Accepted Answer

Look at the inner exception that you are getting. It will tell you which field/property it is having trouble serializing.

You can exclude fields/properties from xml serialization by decorating them with the [XmlIgnore] attribute.

I don't think that XmlSerializer uses the [Serializable] attribute, so I doubt that is the problem.

12/11/2014 8:06:59 AM

Remember that serialized classes must have default (i.e. parameterless) constructors. If you have no constructor at all, that's fine; but if you have a constructor with a parameter, you'll need to add the default one too.

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow