C# 我可以序列化HtmlAgilityPack.HtmlDocument吗

C# 我可以序列化HtmlAgilityPack.HtmlDocument吗,c#,html-agility-pack,C#,Html Agility Pack,代码: 基于异常声明该类未标记为可序列化的事实,我会说“否”。问题:为什么要在会话中存储HtmlDocument对象?为什么不将其存储为文本?基于此异常,该类未标记为可序列化 序列化HtmlDocument的一种方法是创建一个模仿该类的类,将所有属性从一个复制到另一个,并序列化副本。或者,您可以下载源代码,编辑可序列化的HtmlDocument类,然后在项目中重新编译和引用该类 您可以使用诸如之类的类来来回映射属性,这样您就不会自己编写所有映射代码。这是一大堆内容,没有任何解释。。。注意,你的问

代码:


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

基于此异常,该类未标记为可序列化

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


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

这是一大堆内容,没有任何解释。。。注意,你的问题可能会在代码和堆栈跟踪中得到回答。。。我不确定他们是否有其他沟通方式。@m.Babcock:
Console.WriteLine(“我不明白”)
HtmlAgilityPack
是开源的,所以这不是唯一的方法。
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;
                }
            }
        }