我可以在 ViewState 中存储 xmlDocument 对象吗?

发布于 2024-08-15 18:25:29 字数 773 浏览 11 评论 0原文

我有一个 XML 文档,我想将其存储在 ViewState 中,这样在每次回发时我不需要再次从其物理路径加载它。我也不想将它存储在 SessionState 中。

当我尝试在 ViewState 中插入它时,出现错误:

Exception Details: System.Runtime.Serialization.SerializationException: Type 'System.Xml.XmlDocument' in Assembly 'System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken =b77a5c561934e089' 未标记为可序列化。

我的属性是这样的:

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 x​​ml document 可序列化呢?

谢谢

I have one XML document which I want to store it inside ViewState so on each post back I do not need to load it from its physical path again. I do not want to store it in SessionState as well.

when I tried to srote it in ViewState 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 property is 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 then?

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北城挽邺 2024-08-22 18:25:29

您可以将 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() 方法,并在其中添加序列化/反序列化逻辑。

You could serialize your XmlDocument to an XML string and save that string in the ViewState.

Serialization:

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

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

Deserialization:

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

This sort of serialization does not really fit into a get/set property, so you will most likely want to override the SaveViewState() and LoadViewState() methods of your user control/page, and add the serialization/deserialization logic within these.

七度光 2024-08-22 18:25:29

您始终可以将 XML 文档转换为其字符串表示形式,并将其保存/加载到视图状态中。我希望这是一个相对较小的文件?

You could always just convert your XML document to it's string representation and save/load that into viewstate. I hope this is a relatively small document?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文