string StrCartNew = Server.UrlDecode(Request.Cookies[CookieName].Value.ToString()); 
byte[] bt = System.Text.Encoding.Default.GetBytes(StrCartNew); 
Stream smNew = new MemoryStream(bt); 
IFormatter fmNew = new BinaryFormatter(); 
ShopCart SCNew = (ShopCart)fmNew.Deserialize(smNew); 
foreach(ShopCartItem SCI in SCNew.CartItems) 

lblResult.Text += "<br/>产品名称:" + SCI.Name; 

这个我在运行的时候出错。这个错误: 
BinaryFormatter Version incompatibility. Expected Version 1.0. Received Version 623915045.807743536. 
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: BinaryFormatter Version incompatibility. Expected Version 1.0. Received Version 623915045.807743536. 
这行代码有问题 ShopCart SCNew = (ShopCart)fmNew.Deserialize(smNew); 
不知道为什么。

解决方案 »

  1.   

    你把一串字符串存成byte数组,然后再用它反序列化成ShopCart对象。那个字符串数据估计有问题,不能反序列化成目标对象。你当初是怎么保存的呢?
      

  2.   

    是这样保存数据的。
    ShopCart SC = new ShopCart();
                SC.AddItem("1", "ProductName", "ProductName", 0, 0, "ProductID", "", 0, 0, 0, 0, "ShopId", "TestUrl", true);
                SC.AddItem("2", "ProductName123", "ProductName123", 0, 0, "ProductID123", "", 0, 0, 0, 0, "ShopId111", "TestUrl23", true);            //将ShopCart对象写入Cookie
                IFormatter fm = new BinaryFormatter();           
                Stream sm = new MemoryStream();
                fm.Serialize(sm, SC);
                sm.Seek(0, SeekOrigin.Begin);
                StreamReader reader = new StreamReader(sm);
                string strCart = reader.ReadToEnd();
                reader.Close();            
                HttpCookie hc = new HttpCookie(CookieName);
                hc.Value = Server.UrlEncode(strCart);
                hc.Expires = DateTime.Now.AddDays(1);
                Response.Cookies.Add(hc); 
      

  3.   

    不知道这是个什么错误啊!
    BinaryFormatter Version incompatibility. Expected Version 1.0. Received Version 623915045.807743536. 
    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: BinaryFormatter Version incompatibility. Expected Version 1.0. Received Version 623915045.807743536.