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中的序列化和反序列化
推荐阅读