I would like to know the most common scenarios where xml serialization may fail in .NET.
Asked
Active
Viewed 1,773 times
6
-
1Possibly a duplicate of http://stackoverflow.com/questions/67959/net-xml-serialization-gotchas – dthrasher Aug 18 '09 at 14:00
6 Answers
5
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
- 1,026,079
- 266
- 2,566
- 2,900
-
The public members issue can be worked around by using DataContractSerializer – Dmitry Ornatsky Apr 08 '09 at 09:17
-
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 May 11 '09 at 15:34
3
Cannot easily serialize generic collections.
See another question: C# XML Serialization Gotchas

Community
- 1
- 1

Eddie Groves
- 33,851
- 14
- 47
- 48
1
Using the shadows keyword has also broken serialization and deserialization for me because the shadowing causes a new implementation of that property to exist making it incompatible for proper reconstruction. Only use overloads if you want to retype to the specific for a subclass.

Jeremy
- 11
- 1
-
1For those who don't know VB.NET, "`Shadows`" is equivalent to `new` in C#: `public new int PropertyName {get;set;}` where the base class has a `public virtual int PropertyName ...`. – John Saunders Dec 22 '10 at 23:40
0
TimeSpan
objects are not serializable. IDictionary
-implementing types are not serializable either (although they can be serialized with some manual massaging).

Anton Gogolev
- 113,561
- 39
- 200
- 288
0
AFAIK, classes marked as [Obsolete] are not serialized by XmlSerializer since .NET 2.0

Dmitry Ornatsky
- 2,237
- 2
- 18
- 25