我可以在 ViewState 中存储 xmlDocument 对象吗?
我有一个 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 xml 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将
XmlDocument
序列化为 XML 字符串,并将该字符串保存在ViewState
中。序列化:
反序列化:
这种序列化并不真正适合 get/set 属性,因此您很可能希望重写
SaveViewState() 和 LoadViewState()
方法,并在其中添加序列化/反序列化逻辑。You could serialize your
XmlDocument
to an XML string and save that string in theViewState
.Serialization:
Deserialization:
This sort of serialization does not really fit into a get/set property, so you will most likely want to override the
SaveViewState()
andLoadViewState()
methods of your user control/page, and add the serialization/deserialization logic within these.您始终可以将 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?