标签:des style ext color get width
/// <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;
}
序列化和反序列化,布布扣,bubuko.com
序列化和反序列化
标签:des style ext color get width
原文地址:http://www.cnblogs.com/jiangfei1227/p/3784088.html