Can I store xmlDocument object in Session?How to do seralization?

2.6k views Asked by At

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.

2

There are 2 answers

3
Devjosh On

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

0
wizard On

I know this is 2 years old, however I was facing this same problem myself, and since I found a solution, I wanted to share it with you. Devjosh is right, we shouldn't store complex objects in sessions, however sometimes is very usefull, so it is good to know there is a solution.

I noticed that if you store the object like xml/dataset/class, you might experience this error. I tried to store it as a generic object and seems to work. On the same code that was storing a dataset, I had the error, by storing in like generic object, works fine.

Here is a simplified example:

    public static void InsertSessionObject(string item, object obj)
    {
        HttpContext.Current.Session.Add(item, obj);
    }

    public static object GetSessionObject(string item)
    {
        return HttpContext.Current.Session[item];
    }

    public static void RemoveSessionObject(string item)
    {
        HttpContext.Current.Session.Remove(item);
    }

    DataSet lastResults = GetDatasetResults();

    InsertSessionObject("myDataset", lastResults);

    lastResults = (DataSet)GetSessionObject("myDataset");

    RemoveSessionObject("myDataset");