我可以序列化 HtmlAgilityPack.HtmlDocument 吗?

本文关键字:HtmlDocument HtmlAgilityPack 序列化 我可以 | 更新日期: 2023-09-27 18:27:06

Server Error in '/' Application.
Type 'HtmlAgilityPack.HtmlDocument' in Assembly 'HtmlAgilityPack, Version=1.4.0.0, Culture=neutral, PublicKeyToken=bd319b19eaf3b43a' is not marked as serializable.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Runtime.Serialization.SerializationException: Type 'HtmlAgilityPack.HtmlDocument' in Assembly 'HtmlAgilityPack, Version=1.4.0.0, Culture=neutral, PublicKeyToken=bd319b19eaf3b43a' is not marked as serializable.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[SerializationException: Type 'HtmlAgilityPack.HtmlDocument' in Assembly 'HtmlAgilityPack, Version=1.4.0.0, Culture=neutral, PublicKeyToken=bd319b19eaf3b43a' is not marked as serializable.]
   System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers(RuntimeType type) +9449041
   System.Runtime.Serialization.FormatterServices.GetSerializableMembers(Type type, StreamingContext context) +247
   System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo() +160
   System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter, SerializationBinder binder) +473
   System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.Serialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter, SerializationBinder binder) +54
   System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object graph, Header[] inHeaders, __BinaryWriter serWriter, Boolean fCheck) +542
   System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph, Header[] headers, Boolean fCheck) +133
   System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph) +13
   System.Web.UI.ObjectStateFormatter.SerializeValue(SerializerBinaryWriter writer, Object value) +3056
[ArgumentException: Error serializing value 'HtmlAgilityPack.HtmlDocument' of type 'HtmlAgilityPack.HtmlDocument.']
   System.Web.UI.ObjectStateFormatter.SerializeValue(SerializerBinaryWriter writer, Object value) +3371
   System.Web.UI.ObjectStateFormatter.Serialize(Stream outputStream, Object stateGraph) +141
   System.Web.UI.ObjectStateFormatter.Serialize(Object stateGraph) +57
   System.Web.UI.ObjectStateFormatter.System.Web.UI.IStateFormatter.Serialize(Object state) +4
   System.Web.UI.Util.SerializeWithAssert(IStateFormatter formatter, Object stateGraph) +37
   System.Web.UI.HiddenFieldPageStatePersister.Save() +79
   System.Web.UI.Page.SavePageStateToPersistenceMedium(Object state) +108
   System.Web.UI.Page.SaveAllState() +315
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2839

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.272 

法典:

public HtmlDocument HtmlDoc
        {
            get
            {
                if (ViewState["HtmlDoc"] != null)
                    return ViewState["HtmlDoc"] as HtmlDocument;
                else
                {
                    return new HtmlDocument();
                }
            }
            set
            {
                if (value != null)
                {
                    ViewState["HtmlDoc"] = value;
                }
            }
        }

我可以序列化 HtmlAgilityPack.HtmlDocument 吗?

基于异常声明该类未标记为可序列化的事实,我会说"否"。问:为什么要在会话中存储HtmlDocument对象?为什么不将其存储为文本?

根据异常,该类未标记为可序列化。

能够序列化HtmlDocument的一种方法是创建一个模仿该类的类,将所有属性从一个复制到另一个属性并序列化您的副本。或者,您可以下载源代码,编辑 HtmlDocument 类以使其可序列化,然后在项目中重新编译和引用该类

可以使用 AutoMapper 等类来回映射属性,这样您就不会自己编写所有映射代码。