Is it possible to save an xmlDocument object in a ViewState?

I have one XML document that I want to save to ViewState, so in each column I no longer need to load it from its physical path. I also do not want to store it in SessionState.

when I tried to write it to ViewState, I get an error message:

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 property looks something like this:

private XmlDocument MyDocument      {
        get
        {
            object viwObj = ViewState["MyDocument"];
            if (viwObj != null)
                return (XmlDocument)viwObj;

            XmlDocument xmlDoc =  GetMyDocument();
            ViewState["MyDocument"] = xmlDoc;

            return xmlDoc;
        }
    }

How can I make an XML document serializable later?

thank

+3
2

XmlDocument XML ViewState.

:

using (var sw = new StringWriter())
using (var xw = new XmlTextWriter(sw))
{
    xmlDoc.WriteTo(xw);
}

ViewState["MyDocument"] = sw.ToString()

:

var xmlDoc = new XmlDocument();
xmlDoc.LoadXml((string) ViewState["MyDocument"]);

get/set, , , SaveViewState() LoadViewState() / / .

+3

XML- / viewstate. , ?

0

Source: https://habr.com/ru/post/1726439/


All Articles