序列化之对象,字符串,byte数组,XML之间的转换(一)

时间:2021-08-17 06:18:59

工作一年多了,越来越感到自己不能这样一直下去,在最好的青春里面却已经死了。被时间消磨了意志,被工作杀死了精神。我想,我只要活着,我就要去不断的要求自己,不断的去追求更高的山峰。

放眼四周,有趣的灵魂越来越少。独立的人格也越来越稀薄。我们真的不能为了工作而去工作,那样我们活着就和死了一样。生活充满了重复性,我们不断的重复着自己所做一切,上班下班,周末休息。那样活着其实已经死了。我在这一年里面厌恶这种生活。我决定开始新的生活,制定一个计划,每天朝这个方向努力,哪怕用一年两年甚至半辈子,我也是无怨无悔。

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.Xml.Serialization; namespace Test_Control
{
class Program
{
static void Main(string[] args)
{
Object student = new Student() { StudentID = "", StudentName = "zhang" }; string result = ObjectToString<Object>(student);
Console.WriteLine(result + "\r\n"); Student newResult = StringToObject<Student>(result);
Console.WriteLine("ID:{0}, Name:{1}" + "\r\n", newResult.StudentID, newResult.StudentName); //使用UTF8编码将字符串转成byte数组
var byt = SerializeHelper.ConvertToByte("12zhang3");
// Console.WriteLine(BitConverter.ToString(byt)); //使用UTF8编码将byte数组转成字符串
string str = SerializeHelper.ConvertToString(byt);
Console.WriteLine(str); //使用指定字符编码将byte数组转成字符串
string strr = SerializeHelper.ConvertToString(byt, Encoding.Unicode);
Console.WriteLine(strr); //将对象序列化为二进制数据
var obj = new Student() { StudentID = "", StudentName = "zhang" };
var arr = SerializeHelper.SerializeToBinary(obj);
Console.WriteLine(BitConverter.ToString(arr)); //将对象序列化为XML数据 并且打印
var xml = SerializeHelper.SerializeToXml(obj);
Console.WriteLine(BitConverter.ToString(xml)); //将二进制数据反序列化 并且打印
var Bt = new byte[] { , , };
Student obje = SerializeHelper.DeserializeWithBinary<Student>(arr);
Console.WriteLine(obje.StudentName); Console.ReadKey();
} /// <summary>
/// 对象转字符串(序列化后转Base64编码字符串)
/// </summary>
/// <param name="obj">对象</param>
/// <returns>字符串</returns>
public static string ObjectToString<T>(T obj)
{
using (MemoryStream stream = new MemoryStream())
{
IFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, obj);
stream.Position = ;
byte[] buffer = new byte[stream.Length];
stream.Read(buffer, , buffer.Length);
return Convert.ToBase64String(buffer);
}
} /// <summary>
/// 字符串转对象(Base64编码字符串反序列化为对象)
/// </summary>
/// <param name="str">字符串</param>
/// <returns>对象</returns>
public static T StringToObject<T>(string str)
{
using (MemoryStream stream = new MemoryStream())
{
byte[] bytes = Convert.FromBase64String(str);
stream.Write(bytes, , bytes.Length);
stream.Position = ;
IFormatter formatter = new BinaryFormatter();
return (T)formatter.Deserialize(stream);
}
}
} /// <summary>
/// 可序列化的类,用Serializable标示此类可序列化
/// </summary>
[Serializable]
public class Student
{
public string StudentID { get; set; }
public string StudentName { get; set; }
}
public static class SerializeHelper
{
/// <summary>
/// 使用UTF8编码将byte数组转成字符串
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static string ConvertToString(byte[] data)
{
return Encoding.UTF8.GetString(data, , data.Length);
} /// <summary>
/// 使用指定字符编码将byte数组转成字符串
/// </summary>
/// <param name="data"></param>
/// <param name="encoding"></param>
/// <returns></returns>
public static string ConvertToString(byte[] data, Encoding encoding)
{
return encoding.GetString(data, , data.Length);
} /// <summary>
/// 使用UTF8编码将字符串转成byte数组
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static byte[] ConvertToByte(string str)
{
return Encoding.UTF8.GetBytes(str);
} /// <summary>
/// 使用指定字符编码将字符串转成byte数组
/// </summary>
/// <param name="str"></param>
/// <param name="encoding"></param>
/// <returns></returns>
public static byte[] ConvertToByte(string str, Encoding encoding)
{
return encoding.GetBytes(str);
} /// <summary>
/// 将对象序列化为二进制数据
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static byte[] SerializeToBinary(object obj)
{
MemoryStream stream = new MemoryStream();
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(stream, obj); byte[] data = stream.ToArray();
stream.Close(); return data;
} /// <summary>
/// 将对象序列化为XML数据
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public static byte[] SerializeToXml(object obj)
{
MemoryStream stream = new MemoryStream();
XmlSerializer xs = new XmlSerializer(obj.GetType());
xs.Serialize(stream, obj); byte[] data = stream.ToArray();
stream.Close(); return data;
} /// <summary>
/// 将二进制数据反序列化
/// </summary>
/// <param name="data"></param>
/// <returns></returns>
public static object DeserializeWithBinary(byte[] data)
{
//注意,在反序列化的时候,将byte[]写入流以后,需要将游标移到首位,即将Position置零,否则反序列化将报错。
MemoryStream stream = new MemoryStream();
stream.Write(data, , data.Length);
stream.Position = ;
BinaryFormatter bf = new BinaryFormatter();
object obj = bf.Deserialize(stream);
stream.Close(); return obj;
} /// <summary>
/// 将二进制数据反序列化为指定类型对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="data"></param>
/// <returns></returns>
public static T DeserializeWithBinary<T>(byte[] data)
{
return (T)DeserializeWithBinary(data);
} /// <summary>
/// 将XML数据反序列化为指定类型对象
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="data"></param>
/// <returns></returns>
public static T DeserializeWithXml<T>(byte[] data)
{
MemoryStream stream = new MemoryStream();
stream.Write(data, , data.Length);
stream.Position = ;
XmlSerializer xs = new XmlSerializer(typeof(T));
object obj = xs.Deserialize(stream); stream.Close(); return (T)obj;
}
}
}

序列化之对象,字符串,byte数组,XML之间的转换(一)