- Bindary
- Soap
- Xml
- Json
BuildProgrammerList是一个返回值为List的静态方法,主要用途为向List中添加多条信息,List的类型为Person。
1.Binary
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using IOSerialize.IO;
using IOSerialize.Serialize;
namespace IOSerialize
{
internal class Program
{
private static void Main(string[] args)
{
ObjectToBinary();
var binaryResult = BinaryToObject();
}
private static string currentpath = AppDomain.CurrentDomain.BaseDirectory;
//Serialize
private static void ObjectToBinary()
{
var path = Path.Combine(currentpath, "Binary.txt");
var list = DataFactory.BuildProgrammerList();
using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.ReadWrite))
{
BinaryFormatter binary = new BinaryFormatter();
binary.Serialize(fs, list);
}
}
//Deserialize
private static object BinaryToObject()
{
var path = Path.Combine(currentpath, "Binary.txt");
using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
{
BinaryFormatter binary = new BinaryFormatter();
return binary.Deserialize(fs);
}
}
}
}
2.Soap
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Soap;
using IOSerialize.IO;
using IOSerialize.Serialize;
namespace IOSerialize
{
internal class Program
{
private static void Main(string[] args)
{
ObjectToSoap();
var soapResult = SoapToObject();
}
private static string currentpath = AppDomain.CurrentDomain.BaseDirectory;
//Serialize
private static object SoapToObject()
{
var path = Path.Combine(currentpath, "Soap.txt");
using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
{
SoapFormatter soap = new SoapFormatter();
return soap.Deserialize(fs);
}
}
//Deserialize
private static void ObjectToSoap()
{
var path = Path.Combine(currentpath, "Soap.txt");
var list = DataFactory.BuildProgrammerList();
using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.ReadWrite))
{
SoapFormatter soap = new SoapFormatter();
soap.Serialize(fs, list.ToArray());
}
}
}
}
3.Xml
using System;
using System.IO;
using System.Xml.Serialization;
using IOSerialize.IO;
using IOSerialize.Serialize;
namespace IOSerialize
{
internal class Program
{
private static void Main(string[] args)
{
ObjectToXml();
var xmlResult = XmlToObject();
}
private static string currentpath = AppDomain.CurrentDomain.BaseDirectory;
//Serialize
private static object XmlToObject()
{
var path = Path.Combine(currentpath, "XmlSerialize.xml");
using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
{
XmlSerializer xml = new XmlSerializer(typeof(List<Person>));
return xml.Deserialize(fs);
}
}
//Deserialize
private static void ObjectToXml()
{
var path = Path.Combine(currentpath, "XmlSerialize.xml");
var list = DataFactory.BuildProgrammerList();
using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.ReadWrite))
{
XmlSerializer xml = new XmlSerializer(typeof(List<Person>));
xml.Serialize(fs, list);
}
}
}
}
4.Json
Json需要引用System.Web.Extensions或Newtonsoft.Json,前者为微软提供,在引用中就有;后者为第三方提供,可在Nuget中添加或手动导入,在有大数据处理的时候后者的速度高于前者。
using System;
using System.IO;
using System.Web.Script.Serialization;
using IOSerialize.IO;
using IOSerialize.Serialize;
namespace IOSerialize
{
internal class Program
{
private static void Main(string[] args)
{
string json = ObjectToJson();
var jsonResult = JsonToObject(json);
}
private static string ObjectToJson()
{
var list = DataFactory.BuildProgrammerList();
JavaScriptSerializer java = new JavaScriptSerializer();
return java.Serialize(list);
}
private static object JsonToObject(string jsonSource)
{
var list = DataFactory.BuildProgrammerList();
JavaScriptSerializer java = new JavaScriptSerializer();
return java.Deserialize<List<Programmer>>(jsonSource);
}
}
}