Tuesday, December 19, 2006

.NET complaint of the month: XmlDocument is not serializable

I gotta say, on the whole, I'm quite pleased with this .NET Framework that Microsoft has provided us with to develop software with. It's not often that I find something worth complaining about. But this is one of those times.



One of the cool features of .NET is that you can mark a class, or other data structure as serializable, which basically means that the structure and all its data can be easily tossed across a wire, performing a 'deep' copy. This is typically performed by creating an XML representation of that object and its data in a SOAP (Simple Object Access Protocol) envelope, or some binary form.



Now for the irony: The System.Xml.XmlDocument class is not fucking serializable. Yes, that's right. Microsoft's implementation of the very stuff used to serialize arbitrary objects is itself not serializable. I had to implement my interface with strings instead. Figure that out.

3 comments:

Alejandro said...

Yes, I've just smashed to that too,

causing me think some improper thoughts... I'll search for a

workaround for a while, and if unsuccessful, will have to rework some classes. Pityful

szeltan said...

Well, a working workaround for selialising an XmlDocument member (I used it for storing the session in session state server)


[Serializable()]
public class class_name: ISerializable
private XmlDocument xmlCalculation;

public class_name(SerializationInfo info, StreamingContext ctxt){
this.xmlCalculation = new XmlDocument();
if (info.GetString("xmlCalculation") != string.Empty)
this.xmlCalculation.LoadXml(info.GetString("xmlCalculation"));
public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
{
if (xmlCalculation != null)
{ info.AddValue("xmlCalculation", xmlCalculation.OuterXml); }
else
{ info.AddValue("xmlCalculation", string.Empty); }
}
}

szeltan said...

I left out a "}" before the public void GetObjectData( part.

Contributors