欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

unity中如何反序列化xml进行读取

程序员文章站 2022-04-03 12:29:33
...

using System.IO;
using System.Xml.Serialization;
using UnityEngine;

public class SerializeDemo : MonoBehaviour
{
    
    void Start()
    {
        
        XmlReadSerlize();
    }

  
     void XmlReadSerlize()
    {
       var testmlserial =  XmlDeSerialize();
        Debug.Log(testmlserial.ID + "  " + testmlserial.Name);
        foreach (var item in testmlserial._list)
        {
            Debug.Log(item);
        }
    }

    TestSertial XmlDeSerialize()
    {
        string filePath = Application.dataPath + "/demo.xml";
        //FileStream fs = new FileStream(filePath, FileMode.Open,FileAccess.Read,FileAccess.Read);
        try
        {
            FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
            XmlSerializer xml = new XmlSerializer(typeof(TestSertial));
            TestSertial testSertial = (TestSertial)xml.Deserialize(fileStream);
            fileStream.Close();
            return testSertial;
        }
        catch (System.Exception)
        {

            Debug.LogError("null");
            return null;
        }
       
       
    }
}

将序列化的demo.xml打印在控制台了。

相关标签: unity学习 unity