C# 序列化和反序列化 xml 和 binary

序列化其实就是把一个C#类的东西 序列化保存成xml 或者 二进制 格式
首先创建一个类

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Xml.Serialization;
public enum gamestate
{
    gamewait,
    gamestart,
    gameover,
}
[System.Serializable]
public class testserilize{
    [XmlAttribute("id")]//这个是xml 的属性
    public int ID { get; set; }
    [XmlAttribute("Name")]
    public string name { get; set; }
    [XmlElement("List")]//xml节点名字
    public List<int> list { get; set; }
    [XmlElement("enu")]//xml节点名字
    public gamestate game { get; set; }
    [XmlElement]//xml节点名字 如果没有则会以 变量名作为节点名字
    public string nam { get; set; }
 }

下面是序列化和反序列化的代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Xml.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

public class resourcetest : MonoBehaviour {

// Use this for initialization
void Start () {
     // xmlserilize();//xml序列化
    // xmldeserilizerTest(); //测试  xml 反序列化
  //   binary();//二进制序列化
    binaryDesrilizerTest();//测试  二进制反序列化

}

// Update is called once per frame
void Update ()
{
	
}
void xmldeserilizerTest()//xml测试 反序列化得到的结果
{
    testserilize testserilize = xmlDesilize();
    Debug.Log(testserilize.name + "  " + testserilize.ID);
    foreach (int a in testserilize.list)
    {
        Debug.Log(a);
    }
    gamestate shit = testserilize.game;
    Debug.Log(shit);
    Debug.Log(testserilize.nam);
}
void binaryDesrilizerTest()//二进制 反序列化得到的结果
{
    testserilize testserilize = binaryDesirilize();
    Debug.Log(testserilize.name + "  " + testserilize.ID);
    foreach (int a in testserilize.list)
    {
        Debug.Log(a);
    }
    gamestate shit = testserilize.game;
    Debug.Log(shit);
    Debug.Log(testserilize.nam);
}


void xmlserilize()//初始化 创建类
{
    testserilize ts = new testserilize();
    ts.ID = 100;
    ts.name = "测试";
    ts.list = new List<int>();
    ts.list.Add(1);
    ts.list.Add(2);
    ts.game = gamestate.gamestart;
    ts.nam = "学生";
    xmlSerilize(ts);

}
void xmlSerilize(testserilize testserilize)//xml序列化
{

    FileStream fs = new FileStream(Application.dataPath + "/test.xml", FileMode.Create,FileAccess.ReadWrite,FileShare.ReadWrite);
    StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.UTF8);
    XmlSerializer xs = new XmlSerializer(testserilize.GetType());
    xs.Serialize(fs, testserilize);
    sw.Close();
    fs.Close();

    //一下这种写法也可以  可以少写sw 写入流
    //FileStream fs = new FileStream(Application.dataPath + "/test.xml", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
    //XmlSerializer xs = new XmlSerializer(testserilize.GetType());
    //xs.Serialize(fs, testserilize);
    //fs.Close();
}
testserilize xmlDesilize()//xml反序列化
{
    FileStream fs = new FileStream(Application.dataPath + "/test.xml", FileMode.Open,  FileAccess.ReadWrite, FileShare.ReadWrite);
    XmlSerializer xs = new XmlSerializer(typeof(testserilize));
    testserilize testserilize = (testserilize)xs.Deserialize(fs);
    fs.Close();
    return testserilize;
}


void binary()
{
    testserilize testserilize = new testserilize();
    testserilize.ID = 200;
    testserilize.name = "二进制";
    testserilize.list = new List<int>();
    testserilize.list.Add(2);
    testserilize.list.Add(3);
    testserilize.game = gamestate.gameover;
    testserilize.nam = "哈哈哈";
    binarySerilize(testserilize);
}
void binarySerilize(testserilize serilize)//二进制的序列化  利用BinaryFormatter这个类进行序列化和反序列化
{
    FileStream fs = new FileStream(Application.dataPath + "/test.bytes", FileMode.Create, FileAccess.ReadWrite,FileShare.ReadWrite);
    BinaryFormatter bf = new BinaryFormatter();
    bf.Serialize(fs, serilize);
    fs.Close();
}
testserilize binaryDesirilize()//二进制的反序列化
{
    TextAsset testAsset = UnityEditor.AssetDatabase.LoadAssetAtPath<TextAsset>("Assets/test.bytes");
    MemoryStream ms = new MemoryStream(testAsset.bytes);//将文本读取到这个MemoryStream流下面
    BinaryFormatter bf = new BinaryFormatter();
    testserilize testserilize = (testserilize)bf.Deserialize(ms);
    ms.Close();
    return testserilize;
}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值