本帖最后由 张少甫 于 2012-9-21 17:53 编辑
/// <summary> /// 序列化 /// </summary> /// <typeparamname="T">序列化类型</typeparam> /// <paramname="t">实例</param> /// <returns></returns> public static stringEncode<T>(T t) { MemoryStreamstream=new MemoryStream(); XmlSerializerxml=new XmlSerializer(typeof(T)); xml.Serialize(stream, t);//把序列化的信息写入流中。 byte[]by = stream.GetBuffer(); stream.Close(); return Encoding.UTF8.GetString(by, 0, by.Length); } /// <summary> /// 反序列化 /// </summary> /// <typeparamname="T">序列化类型</typeparam> ///<param name="text">信息/param> /// <returns></returns> public static T Decode<T>(stringtext) { byte[]by=Encoding.UTF8.GetBytes(text); MemoryStreamstream=new MemoryStream(by); XmlSerializerxml = new XmlSerializer(typeof(T)); T t=(T) xml.Deserialize(stream); Stream.Close(); return t; } |