标签:序列化
序列化,就是转换格式,将一个String,或者一个对象,或者一个List,转换成流的形式,文本流(XML、TXT、Dat……)或者是二进制流,在转换回去就是反序列化。
这就是转换为二进制流。
(1)对象所属类加上标志[Serializable],在属性前加入标志 [XmlAttribute](加一个即可)
如图:
(2)需要有一个常规的构造
static byte[] ObjectSerializerByte<T>(T obj)
{
var stream = new MemoryStream();
var binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(stream, obj);
return stream.GetBuffer();
}
第二种方法
byte[] CatalogSerializerBinaryToStream<T>(T obj)
{
Stream stream = new MemoryStream();
BinaryFormatter binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(stream, obj);
BinaryReader binary = new BinaryReader(stream);
binary.BaseStream.Seek(0, SeekOrigin.Begin);
byte[] fileBytes = new byte[stream.Length];
fileBytes = binary.ReadBytes((int)binary.BaseStream.Length);
stream.Close();
return fileBytes;
}
public static T ByteDeserializerCatalog<T>(byte[] binaryBytes)
{
var bfBinaryFormatter = new BinaryFormatter();
return (T)bfBinaryFormatter.Deserialize(new MemoryStream(binaryBytes));
}
我这个方法封装的,第4个参数很少用过,如果是null,新建文本,如果是apend,在传入路径打开的文本中添加内容,续写
/// <summary>
/// 对象序列化为XML、Txt等文本格式
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
/// <param name="localPath">本地路径</param>
/// <param name="fileType">文件类型(xml\txt)</param>
/// <param name="fileModel">打开文件的方式(null直接create||append本地若存在在文件尾添加)</param>
/// 在append中可以延伸更改编码方式
void CatalogSerializerXmlToLocal<T>(T obj, string localPath, string fileType, string fileModel)
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
TextWriter writer = null;
switch (fileModel)
{
case null: { writer = new StreamWriter(localPath + "." + fileType); break; }
case "append":
{
FileStream stream = new FileStream(localPath + "." + fileType, FileMode.Append);
writer = new StreamWriter(stream);
break;
}
}
if (null != writer )
{
serializer.Serialize(writer, obj);
writer.Close();
}
}
T XmlDeserializerCatalog<T>(T obj, string localPath)
{
XmlSerializer serializer = new XmlSerializer(obj.GetType());
FileStream stream = new FileStream(localPath, FileMode.Open);
obj = (T)serializer.Deserialize(stream);
stream.Close();
return obj;
}
private XmlDocument RootDataBaseInStreamDeserializationBecomeXml(byte[] strBytes)
{
XmlSerializer ser = new XmlSerializer(typeof(XmlDocument));
MemoryStream memory = new MemoryStream(strBytes);
XmlDocument xmlDocument = (XmlDocument)ser.Deserialize(memory);
return xmlDocument;
}
byte[] ListaySerializeByte<T>(List<T> obj)
{
var bf = new BinaryFormatter();
var ms = new MemoryStream();
bf.Serialize(ms, PipeRangeGeometrys);
return ms.ToArray();
}
object BytesDeserializeObject(byte[] arrBytes)
{
var memStream = new MemoryStream();
var binForm = new BinaryFormatter();
memStream.Write(arrBytes, 0, arrBytes.Length);
memStream.Seek(0, SeekOrigin.Begin);
var obj = (Object)binForm.Deserialize(memStream);
return obj;
}
return的强转为List< T >即可
List< T >与XML,附带String,将List< T >序列化为XML,然后转为String……,在转回List< T >
var ser = new XmlSerializer(typeof(List< T >));
var sb = new StringBuilder();
var sw = new StringWriter(sb);
ser.Serialize(sw, entList);//entList是要转换的
string entListToString = sb.ToString();
var sr = new StringReader(str);//str是要转换的
var ser = new XmlSerializer(typeof(List<string>));
List<string> list = (List<string>) ser.Deserialize(sr);
一些是从项目中摘取,一些是没有用到但自己没事总结,但都是亲测过的,部分方法名字起的不够恰当~~
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:序列化
原文地址:http://blog.csdn.net/panpen120/article/details/48081075