首页 > 代码库 > [Unity 笔记] unity中如何将Object序列化成xml字符串并保存
[Unity 笔记] unity中如何将Object序列化成xml字符串并保存
需要使用到以下这几个c#内置的命名空间。
using System.Xml;
using System.IO;
using System.Xml.Serialization;
序列化并存储xml文件
XmlTextWriter xWrite = new XmlTextWriter(filename, null); XmlSerializer sl = new XmlSerializer(type); sl.Serialize(xWrite, target); xWrite.Close();
XmlReader xRead = new XmlTextReader(filename); XmlSerializer sl = new XmlSerializer(type); object obj = sl.Deserialize(xRead); xRead.Close(); return obj;
完整源码如下:
using UnityEngine; using System.Collections; using System.Xml; using System.IO; using System.Xml.Serialization; namespace CharacterBase { public class XmlIO { public static object LoadFromXml(System.Type type, string filename) { XmlReader xRead = new XmlTextReader(filename); XmlSerializer sl = new XmlSerializer(type); object obj = sl.Deserialize(xRead); xRead.Close(); return obj; } public static void SaveToXml(string filename, System.Type type, object target) { XmlTextWriter xWrite = new XmlTextWriter(filename, null); XmlSerializer sl = new XmlSerializer(type); sl.Serialize(xWrite, target); xWrite.Close(); } } }
[Unity 笔记] unity中如何将Object序列化成xml字符串并保存
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。