您的位置:首页 > 其它

LINQ Entity to store in ViewState or Serialize

2009-08-17 17:34 295 查看
Recently, I was trying to store some temp data in ViewState, and I was using LINQ's Entity, and I found it errored by couldn't serialized the LINQ's entity. After some searching, I found it needed to set a property of DataContext file calling 'Serialization Mode' to value 'Unidirectional', this seems it was specified for WCF, for the setting will add [DataContact],[DataMember] attributes for class or class's properties.

After that, You'll using class 'DataContractSerializer' to do serialization.

DataContractSerializer dcs = new DataContractSerializer(typeof(List<user>));
StringBuilder sb = new StringBuilder();
XmlWriter xw = XmlWriter.Create(sb);
dcs.WriteObject(xw, comments);
xw.Close();
ViewState["ds"] = sb.ToString();
when you want to deserialize from ViewState, using

string content = ViewState["ds"] as string;
DataContractSerializer dsc = new DataContractSerializer(typeof(List<user>));
MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(content));
object obj = dsc.ReadObject(ms);
the above codes, seem not so good, so, any comment is welcome for better solution.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐