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


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)