Take the 2-minute tour ×

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?

share|improve this question

15 Answers 15

up vote 265 down vote accepted

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.

share|improve this answer
My object had a Uri field, which caused this exception; the Uri class does not have a parameterless constructor. Thanks for the tip. –  ford Oct 28 '11 at 21:45
Came across this with a google search - my particular issue was having a property in my "to be serialized" class as IList when it needed to be List. –  Paul Aldred-Bann Oct 8 '12 at 13:06
How does one look at an "inner exception"? –  David Dec 17 '12 at 3:47
or add '@exception' to a watch –  arolson101 Jun 13 '13 at 17:30
Thanks, this answer helped me out. I initially looked at the inner exception, and just saw mentioning of the main class. But I realised I could drill down into the innerexceptions of the innrexceptions, and eventually, 5 levels down, I found the issue. I had classes that were conflicting. Thanks. –  Louis van Tonder Jul 12 '13 at 15:12

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.

share|improve this answer
Thanks for the reminder! I hate that this is a runtime error with little explanation. –  Jared Updike Nov 17 '08 at 23:33
Just struggled a bunch with this error too. Thanks Jeremy for the point! –  urbanspr1nter Feb 7 '13 at 23:27
This just saved me a bunch of time! cheers jeremy –  Shane.C Apr 26 '13 at 11:17
WoW!! Saved my day. –  Hamzeh Soboh Mar 17 at 12:43
I keep on making this mistake over and over again. thanks for reminding me to use a parameterless constructor ^^ –  aZtraL-EnForceR May 31 at 19:50

I had a similar problem, and it turned out that the serializer could not distinguish between 2 classes I had with the same name (one was a subclass of the other). The inner exception looked like this:

'Types BaseNamespace.Class1' and 'BaseNamespace.SubNamespace.Class1' both use the XML type name, 'Class1', from namespace ''. Use XML attributes to specify a unique XML name and/or namespace for the type.

Where BaseNamespace.SubNamespace.Class1 is a subclass of BaseNamespace.Class1.

What I needed to do was add an attribute to one of the classes (I added to the base class):


Note: If you have more layers of classes you need to add an attribute to them as well.

share|improve this answer
This fixed the problem for me, thank you, +1; I had a similar setting with several Processor* objects, each one with a Config inner class. The runtime was not able to distinguish between SomeNS.Processor1.Config and SomeNS.Processor2.Config. –  damix911 Jun 3 '12 at 20:38

Also be aware that XmlSerializer cannot serialize abstract properties.. See my question here (which I have added the solution code to)..

XML Serialization and Inherited Types

share|improve this answer

All the objects in the serialization graph have to be serializable.

Since XMLSerializer is a blackbox, check these links if you want to debug further into the serialization process..

Changing where XmlSerializer Outputs Temporary Assemblies

HOW TO: Debug into a .NET XmlSerializer Generated Assembly

share|improve this answer

I've discovered that the Dictionary class in .Net 2.0 is not serializable using XML, but serializes well when binary serialization is used.

I found a work around here.

share|improve this answer

I too thought that the Serializable attribute had to be on the object but unless I'm being a complete noob (I am in the middle of a late night coding session) the following works from the SnippetCompiler:

using System;
using System.IO;
using System.Xml;
using System.Collections.Generic;
using System.Xml.Serialization;

public class Inner
    private string _AnotherStringProperty;
    public string AnotherStringProperty 
      get { return _AnotherStringProperty; } 
      set { _AnotherStringProperty = value; } 

public class DataClass
    private string _StringProperty;
    public string StringProperty 
       get { return _StringProperty; } 
       set{ _StringProperty = value; } 

    private Inner _InnerObject;
    public Inner InnerObject 
       get { return _InnerObject; } 
       set { _InnerObject = value; } 

public class MyClass

    public static void Main()
    		XmlSerializer serializer = new XmlSerializer(typeof(DataClass));
    		TextWriter writer = new StreamWriter(@"c:\tmp\dataClass.xml");
    		DataClass clazz = new DataClass();
    		Inner inner = new Inner();
    		inner.AnotherStringProperty = "Foo2";
    		clazz.InnerObject = inner;
    		clazz.StringProperty = "foo";
    		serializer.Serialize(writer, clazz);


I would imagine that the XmlSerializer is using reflection over the public properties.

share|improve this answer

If you need to handle specific attributes (i.e. Dictionary, or any class), you can implement the IXmlSerialiable interface, which will allow you more freedom at the cost of more verbose coding.

public class NetService : IXmlSerializable
#region Data

public string Identifier = String.Empty;

public string Name = String.Empty;

public IPAddress Address = IPAddress.None;
public int Port = 7777;


#region IXmlSerializable Implementation

public XmlSchema GetSchema() { return (null); }

public void ReadXml(XmlReader reader)
// Attributes
Identifier = reader[XML_IDENTIFIER];
if (Int32.TryParse(reader[XML_NETWORK_PORT], out Port) == false)
throw new XmlException("unable to parse the element " + typeof(NetService).Name + " (badly formatted parameter " + XML_NETWORK_PORT);
if (IPAddress.TryParse(reader[XML_NETWORK_ADDR], out Address) == false)

public void WriteXml(XmlWriter writer)
    // Attributes
    writer.WriteAttributeString(XML_IDENTIFIER, Identifier);
    writer.WriteAttributeString(XML_NETWORK_ADDR, Address.ToString());
    writer.WriteAttributeString(XML_NETWORK_PORT, Port.ToString());

private const string XML_IDENTIFIER = "Id";

private const string XML_NETWORK_ADDR = "Address";

private const string XML_NETWORK_PORT = "Port";


There is an intersting article, which show an elegant way to implements a sophisticated way to "extend" the XmlSerializer.

The article say:

IXmlSerializable is covered in the official documentation, but the documentation states it's not intended for public use and provides no information beyond that. This indicates that the development team wanted to reserve the right to modify, disable, or even completely remove this extensibility hook down the road. However, as long as you're willing to accept this uncertainty and deal with possible changes in the future, there's no reason whatsoever you can't take advantage of it.

Because this, I suggest to implement you're own IXmlSerializable classes, in order to avoid too much complicated implementations.

...it could be straightforward to implements our custom XmlSerializer class using Reflection.

share|improve this answer

I recently got this in a web reference partial class when adding a new property. The auto generated class was adding the following attributes.

    [System.Xml.Serialization.XmlElementAttribute(Order = XX)]

I needed to add a similar attribute with an order one higher than the last in the auto generated sequence and this fixed it for me.

share|improve this answer

Can you describe DataClass? all its properties in detail. You may have used a generic type which is not supported.

share|improve this answer

I just got the same error and discovered that a property of type "IEnumerable" was the problem. It appears that IEnumerables cannot be serialized directly.

share|improve this answer

I had a situation where the Order was the same for two elements in a row

[System.Xml.Serialization.XmlElementAttribute(IsNullable = true, Order = 0, ElementName = "SeriousInjuryFlag")]

.... some code ...

[System.Xml.Serialization.XmlElementAttribute(IsNullable = true, Order = 0, ElementName = "AccidentFlag")]

When I changed the code to increment the order by one for each new Property in the class, the error went away.

share|improve this answer

Also note that you cannot serialize user interface controls and that any object you want to pass onto the clipboard must be serializable otherwise it cannot be passed across to other processes.

share|improve this answer

I have been using the NetDataSerialiser class to serialise my domain classes. NetDataContractSerializer Class.

The domain classes are shared between client and server.

share|improve this answer

Most common reasons by me:

 - the object being serialized has no parameterless constructor
 - the object contains Dictionary
 - the object has some public Interface members
share|improve this answer

Your Answer


