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
2009-04-08 09:13:58
+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
2009-04-08 09:15:33
The public members issue can be worked around by using DataContractSerializer
Dmitry Ornatsky
2009-04-08 09:17:03
true - will clarify...
Marc Gravell
2009-04-08 09:18:04
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
2009-05-11 15:34:30
+1
A:
Depending on the serializer, cyclic references may not work
Sebastian Sedlak
2009-04-08 09:17:27
A:
AFAIK, classes marked as [Obsolete] are not serialized by XmlSerializer since .NET 2.0
Dmitry Ornatsky
2009-04-08 09:18:13
+3
A:
Cannot easily serialize generic collections.
See another question: C# XML Serialization Gotchas
eddiegroves
2009-04-08 09:20:11