Newtonsoft.Json
序列化的内容不只是类的对象,
数组也可以
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
class Class1
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: 在此处添加代码以启动应用程序
//
PurchaseOrder obj = new PurchaseOrder();
obj.ItemsOrders=new Item[100];
obj.ItemsOrders[0]=new Item();
obj.ItemsOrders[0].ItemID="First";
obj.ItemsOrders[0].ItemPrice=500.25M;
obj.ItemsOrders[1]=new Item();
obj.ItemsOrders[1].ItemID="Second";
obj.ItemsOrders[1].ItemPrice=66.88M;
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream("MyFile.bin", FileMode.Create, FileAccess.Write, FileShare.None);
formatter.Serialize(stream, obj);
stream.Close();
}
[Serializable]
public class PurchaseOrder
{
public Item [] ItemsOrders;
}
[Serializable]
public class Item
{
public string ItemID;
public decimal ItemPrice;
}
} |