Rants and Raves

Thanks for visiting my blog!

Serializing XmlDocuments in the .NET Framework
Serializing XmlDocuments in the .NET Framework
August 25, 2002

I was recently in a DevelopMentor course when I ran into a very interesting observation.  The XmlSerializer serializes any class that dervies from XmlNode (including XmlDocument, XmlElement, et al) as plain XML.  Previous to RTM of the .NET framework, these classes were serialized like any other class (all public properties and fields were serialized).  To our amazement (Dan Sullivan and mine), we realized that the XML classes serialized perfectly when run through the XmlSerializer class.

Ok, neato but why do I care?  As a developer using ADO.NET, I realized that by utilizing the XmlDataDocument and specifying an XSD for my DataSet, I could have my Web Services return an XML as specified by the XSD without ever doing transformation of the data going out of the Web Service.

This works because the .NET Web Services infrastructure uses the XmlSerializer to serialize objects.  Normally, when specifying .NET types as arguments or return types for your Web Services, this would cause problems with interoperability with non-.NET platforms.  So if I specify a WebMethod like:

[WebMethod]
public XmlDataDocument MyWebService(string foo, string bar)
{
  ...
}

The result of the Web Service can return an XML document that maps directly to a specific XSD without getting our hands dirty with translating our DataSet data.