views:

336

answers:

5

I would like to know the most common scenarios where xml serialization may fail in .NET.

A: 

TimeSpan objects are not serializable. IDictionary-implementing types are not serializable either (although they can be serialized with some manual massaging).

Anton Gogolev
+4  A: 

I'm thinking mainly of XmlSerializer here:

  • it is limited to tree-like data; it can't handle full object graphs
  • it is limited to public members, on public classes
  • it can't really do much with object members
  • it has some weaknesses around generics
  • like many serializers, it won't touch instance properties on a collection (bad practice in the first place)
  • xml simply isn't always a good choice for large data (not least, for performance)
  • requires a public parameterless constructor

DataContractSerializer solves some of these, but has its own limitations:

  • it can't handle values in attributes
  • requires .NET 3.0 (so not much use in 2.0)
Marc Gravell
The public members issue can be worked around by using DataContractSerializer
Dmitry Ornatsky
true - will clarify...
Marc Gravell
These are all good things to know about the Xml Serialization framework in .NET, but I would not say they are common scenarios where XML Serialization "fails." For example, that the Xml serializer serializes only public read/write members is an attribute of the serialization framework, not a scenario where it fails. That it requires the class to have a public parameterless ctor is also not a "failure" per se.
Cheeso
+1  A: 

Depending on the serializer, cyclic references may not work

Sebastian Sedlak
A: 

AFAIK, classes marked as [Obsolete] are not serialized by XmlSerializer since .NET 2.0

Dmitry Ornatsky
+3  A: 

Cannot easily serialize generic collections.

See another question: C# XML Serialization Gotchas

eddiegroves