Unity xml序列化和反序列化

2021-06-16  本文已影响0人  ___________6a1d

xml序列化

也就是将一个类序列化本地数据,例如以下类:

using System.Collections.Generic;
using System.Xml.Serialization;
 
[System.Serializable]
public class TestSerilize
{
  
  //XmlAttribute 这个是序列化标签,有这些标签才能序列化
    [XmlAttribute("ID")]
    public int ID { get; set; }
    [XmlAttribute("Name")]
    public string Name { get; set; }
    [XmlElement("GradeList")]//也可用[][XmlArray("GradeList")]
    public List<int> GradeList { get; set; }
}

实际使用测试,将类转化为xml:

using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Xml.Serialization;
 
public class Test : MonoBehaviour
{
    void Start()
    {
        SerilizeTest();
    }
    /// <summary>
    /// 初始化一个testSerilize,测试序列化xml
    /// </summary>
    void SerilizeTest()
    {
        TestSerilize testSerilize = new TestSerilize();
        testSerilize.ID = 1;
        testSerilize.Name = "测试";
        testSerilize.GradeList = new List<int>();
        testSerilize.GradeList.Add(80);
        testSerilize.GradeList.Add(90);
 
        XMLSerilize(testSerilize);
    }
    /// <summary>
    /// 序列化xml
    /// </summary>
    /// <param name="testSerilize"></param>
    void XMLSerilize(TestSerilize testSerilize)
    {
        //创建xml文件流
        FileStream fileStream = new FileStream(Application.dataPath + "/Test.xml", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
        //写入流,用来写入数据
        StreamWriter streamWriter = new StreamWriter(fileStream, System.Text.Encoding.UTF8);
        //用想要序列化的类型得到序列化后的xml
        XmlSerializer xmlSerializer = new XmlSerializer(testSerilize.GetType());
        //将testSerilize序列化到streamWriter
        xmlSerializer.Serialize(streamWriter, testSerilize);
        //流使用后一定要记得关闭,或者使用using(){}
        fileStream.Close();
        streamWriter.Close();
    }

(刷新unity,缩小后放大就可以)最后得到:


image.png

xml反序列化

也就是将一个xml文件反向序列化成对象,操作代码:

using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Xml.Serialization;
 
public class Test : MonoBehaviour
{
    void Start()
    {
        //SerilizeTest();
        DeSerilizeTest();
    }
    /// <summary>
    /// 初始化一个testSerilize,测试序列化xml
    /// </summary>
    void SerilizeTest()
    {
        TestSerilize testSerilize = new TestSerilize();
        testSerilize.ID = 1;
        testSerilize.Name = "测试";
        testSerilize.GradeList = new List<int>();
        testSerilize.GradeList.Add(80);
        testSerilize.GradeList.Add(90);
 
        XMLSerilize(testSerilize);
    }
    /// <summary>
    /// 测试反向序列化xml
    /// </summary>
    void DeSerilizeTest()
    {
        TestSerilize testSerilize = XMLDeserilize();
 
        Debug.Log("ID===="+testSerilize.ID);
        Debug.Log("Name====" + testSerilize.Name);
        foreach (int item in testSerilize.GradeList)
        {
            Debug.Log("Grade===" + item);
        }
    }
    /// <summary>
    /// 序列化xml
    /// </summary>
    /// <param name="testSerilize"></param>
    void XMLSerilize(TestSerilize testSerilize)
    {
        //创建xml文件流
        FileStream fileStream = new FileStream(Application.dataPath + "/Test.xml", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
        //写入流,用来写入数据
        StreamWriter streamWriter = new StreamWriter(fileStream, System.Text.Encoding.UTF8);
        //用想要序列化的类型得到序列化后的xml
        XmlSerializer xmlSerializer = new XmlSerializer(testSerilize.GetType());
        //将testSerilize序列化到streamWriter
        xmlSerializer.Serialize(streamWriter, testSerilize);
        //流使用后一定要记得关闭,或者使用using(){}
        fileStream.Close();
        streamWriter.Close();
    }
    /// <summary>
    /// 反序列化xml
    /// </summary>
    /// <returns></returns>
    TestSerilize XMLDeserilize()
    {
        //创建xml文件流,第二个参数和上边不同
        FileStream fileStream = new FileStream(Application.dataPath + "/Test.xml", FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
        //用想要序列化的类型得到序列化后的xml
        XmlSerializer xmlSerializer = new XmlSerializer(typeof(TestSerilize));
        TestSerilize testSerilize = (TestSerilize)xmlSerializer.Deserialize(fileStream);
        fileStream.Close();
        return testSerilize;
    }
}

结果运行如下图:


image.png

后记:类与xml相互转化,实际上不用,太明显,数据量也大。

上一篇下一篇

猜你喜欢

热点阅读