XmlDocument Is Still Not Serializable in BizTalk 2009, .NET
For those who are not already in the know on this, the System.Xml.XmlDocument object that we all know and love is, ironically, not serializable. Certainly a mistake or misunderstanding somewhere from Microsoft land, but as of version 3.5 of .NET, it’s still missing this capability. You would think…
Anyway, this one has really caused trouble for me in BizTalk as BizTalk serializes darn near everything. Where this became a problem has been in the creation of .NET components that are called from orchestration.
If you choose to use a class that has an XmlDocument object as one of its members, you’ll find the message
"Type 'System.Xml.XmlDocument' in Assembly 'System.Xml, Version=126.96.36.199, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable."
This occurs anytime that BTS attempts to serialize a class that uses an XmlDocument as an instance member. Oddly enough, this won’t apply to variables that are of the XmlDocument type in the orchestration as BTS knows it’s not serializable and won’t try.
There are a few ways around this dilemma:
1. Mark the member as NonSerializable. BTS will not attempt to serialize the object and you shouldn’t see this error.
2. Create all methods that return XmlDocument types as static members. No instance == no persistence == no serialization.
3. Return XmlNodes instead. An XmlDocument can easily be converted to an XmlNode object which is serializable.
4. Make sure there are no persistence points in your orchestration. <not likely>.
Oh, and don’t forget to GAC your components and restart your Host Instances…