首页 > 代码库 > 序列化和反序列化
序列化和反序列化
/// <summary>
/// xml序列化
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
/// <returns></returns>
public static string SaveXmlFromObj<T>(T obj)
{
try
{
if (obj == null) return null;
XmlSerializer serializer;
serializer = new XmlSerializer(typeof(T));
MemoryStream stream = new MemoryStream();
XmlTextWriter xtw = new XmlTextWriter(stream, Encoding.UTF8);
xtw.Formatting = Formatting.Indented;
try
{
serializer.Serialize(stream, obj);
}
catch { return null; }
stream.Position = 0;
string returnStr = string.Empty;
using (StreamReader sr = new StreamReader(stream, Encoding.UTF8))
{
string line = "";
while ((line = sr.ReadLine()) != null)
{
returnStr += line;
}
}
return returnStr;
}
catch (Exception ex)
{
return null;
}
}
/// <summary>
/// xml反序列化
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="data"></param>
/// <returns></returns>
public static T LoadObjFromXML<T>(string data)
{
using (MemoryStream stream = new MemoryStream())
{
using (StreamWriter sw = new StreamWriter(stream, Encoding.UTF8))
{
sw.Write(data);
sw.Flush();
stream.Seek(0, SeekOrigin.Begin);
XmlSerializer serializer = new XmlSerializer(typeof(T));
try
{
return ((T)serializer.Deserialize(stream));
}
catch { return default(T); }
}
}
}
/// <summary>
/// Dictionary序列化
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
/// <returns></returns>
public static string DicToXml<T>(T obj)
{
try
{
if (obj == null) return null;
DataContractSerializer dataSerializer;
dataSerializer = new DataContractSerializer(typeof(T));
var ms = new MemoryStream();
try
{
dataSerializer.WriteObject(ms, obj);
}
catch { return null; }
ms.Position = 0;
var sr = new StreamReader(ms);
var str = sr.ReadToEnd();
return str.ToString();
}
catch
{
return null;
}
}
/// <summary>
/// Dictionary反序列化
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="data"></param>
/// <returns></returns>
public static T XmlToDic<T>(string data)
{
DataContractSerializer dataSerializer;
dataSerializer = new DataContractSerializer(typeof(T));
var buffer = System.Text.Encoding.UTF8.GetBytes(data);
var ms = new MemoryStream(buffer);
try
{
return (T)dataSerializer.ReadObject(ms);
}
catch
{
return default(T);
}
}
/// <summary>
/// 二进制把对象序列化并返回相应的字节
/// </summary>
/// <param name="pObj">需要序列化的对象</param>
/// <returns>byte[]</returns>
public byte[] SerializeObject(object pObj)
{
if (pObj == null)
return null;
System.IO.MemoryStream _memory = new System.IO.MemoryStream();
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(_memory, pObj);
_memory.Position = 0;
byte[] read = new byte[_memory.Length];
_memory.Read(read, 0, read.Length);
_memory.Close();
return read;
}
/// <summary>
/// 二进制把字节反序列化成相应的对象
/// </summary>
/// <param name="pBytes">字节流</param>
/// <returns>object</returns>
public object DeserializeObject(byte[] pBytes)
{
object _newOjb = null;
if (pBytes == null)
return _newOjb;
System.IO.MemoryStream _memory = new System.IO.MemoryStream(pBytes);
_memory.Position = 0;
BinaryFormatter formatter = new BinaryFormatter();
_newOjb = formatter.Deserialize(_memory);
_memory.Close();
return _newOjb;
}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。