美文网首页
Unity xml序列化和反序列化

Unity xml序列化和反序列化

作者: ___________6a1d | 来源:发表于2021-06-16 11:33 被阅读0次

    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相互转化,实际上不用,太明显,数据量也大。

    相关文章

      网友评论

          本文标题:Unity xml序列化和反序列化

          本文链接:https://www.haomeiwen.com/subject/niuzeltx.html