Why the DataTable not?
This is a recent question I've found in my inbox.
Both the DataSet and the DataTable implement ISerializable. Why can't a DataTable be returned from a Web
Service? I've come across some pretty vague explanations through Google and MSDN but I'm determined to understand the true technical reason why.
The reason relates to the class that ultimately handles the XML (de)serialization of any .NET type used with a Web service method. This class is XmlSerializer and serializes .NET types to XML. It is fairly different from a .NET formatter and designed to serve different purposes. XmlSerializer is NOT designed to persist the state of an object (like formatters do), but simply to move a snapshot of the constituent data of the object. For this reason, it only moves public members and doesn't even attempt to handle circular references.
XmlSerializer is a class specific of the .NET Web service implementation and could have been kept private as well. You should not use it as an XML formatter. (Looks like a similar class will make its debut with Whidbey.) Because it is not a formatter, XmlSerializer doesn't know anything about the ISerializable interface. In fact, XmlSerializer and formatters push two (radically) different models of serialization with different purposes and features.
So what's XmlSerializer all about? It just transforms any .NET type used as an I/O parameter of a Web service method into an XSD type and vice versa.
In terms of practical features, XmlSerializer doesn't handle circular references and skips over protected and private members. This is its default behavior. According to this, neither the DataSet nor the DataTable are good at
serializing through the XmlSerializer. So why is it supporting only the DataSet? It's a sort of exception.
The XmlSerializer works with simple classes (no circular refs) and with classes that implement IXmlSerializable
(an interface not fully documented in v1.x). The DataSet does implement it; the DataTable does not.
For details, look for the types.cs file in the SSCLI v2 (sscli\fx\src\xml\system\newxml\serialization path) to have an idea of what types are really supported. There are a few more "exceptions" than just the DataSet.