I have to many classes that contains some field and properties of type XmlDocument
.
When I put the objects of these classes in session (such as state Server, SQL State Server) it is necessary to serialize them.
But if we have a property of type XmlDocument and add [Serialize]
Attribute above our class, the following error will be appears .
Unable to serialize the session state. In 'StateServer' and 'SQLServer' mode, ASP.NET will serialize the session state objects, and as a result non-serializable objects or MarshalByRef objects are not permitted. The same restriction applies if similar serialization is done by the custom session state store in 'Custom' mode.
this error does not appears for the fields have [NonSerialize]
attribute. the properties can’t have the attribute [NonSerialize]
because it can be used only for class and struct and event and delegate.
Internally, according to the docs, the state server uses
BinaryFormatter
to serialize complex types.BinaryFormatter
serializes all public and private fields (not properties!) of a class or struct that is marked as[Serializable]
. ButXmlDocument
, as you have noted, is not so marked, thus cannot be immediately serialized withBinaryFormatter
.XmlDocument
can, however, be trivially converted from and to a string -- the XML itself that the document represents. Thus if theXmlDocument
field were contained in a type implementingISerializable
, then itsGetObjectData()
could simply store the corresponding XML string inside the serialization stream. Then the corresponding serialization constructor could extract the XML string and reconstitute theXmlDocument
.Since implementing
ISerializable
on a pre-existing class can be time consuming, the easiest way to accomplish what you want would be to introduce a small serialization wrapper struct for your XML documents:Then, in the classes you want to serialize, replace your
XmlDocument
fields (and auto-implemented properties) with wrapper struct fields, e.g.:The implicit operators in the struct handle automatic conversion from and to the wrapper.