I have one XML document which I want to store it inside session so on each post back I do not need to load it from its physical path again. We are using state server.
When I tried to store it in Session I get an error:
Exception Details: System.Runtime.Serialization.SerializationException: Type 'System.Xml.XmlDocument' in Assembly 'System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable.
My code is something like this:
string resumeSection = string.Empty;
resumeSection = resume.GetXMLSection(1)
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(resumeSection);
Session["USERXML"] = xmloc;
How to do seralization?
As I am getting below error 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.
When you store any object in Session it should be marked
[serealizable]
so you should serialize your Object before storing it to session or viewstate.to be honest you shouldn't really be putting complex types into session state you should only store simple types or light weight business entities not objects like
XmlDocument
.I think the best way to go would be to use custom serialization. If the class is not too big, you can persist the XmlDocument to a string and then just store that value when serializing the instance. Then, when de-serializing, you can just pull that from the
SerializationInfo
instance.you can get quick idea from here
this past SO post may also answer your question to some extent