对象序列化成XML存储,XML反序列换成对象
2015-12-11 19:58
309 查看
using UnityEngine; using System.Collections; using System.Xml; using System.Xml.Serialization; using System.IO; using System.Text; public static class GameDataMgr { static public T LoadData<T>(string name,string path) { T result=default(T); string str_Data = LoadXML(name,path); if (string.IsNullOrEmpty(str_Data) == true) { return result; } result = DeserializeObject<T>(str_Data); return result; } static public void SaveData<T>(T data,string mPath,string mFileName) { string str_Data = SerializeObject(data); SaveXML(str_Data,mPath,mFileName); } static private void SaveXML(string data,string path,string fileName) { StreamWriter sw; FileInfo fi = new FileInfo(path + "/" + fileName); if (fi.Exists == false){ sw = fi.CreateText(); } else{ fi.Delete(); sw = fi.CreateText(); } sw.Write(data); sw.Close(); Debug.Log("SaveXML:" + data); } static private string LoadXML(string fileName,string path) { string str_FilePath = path + "/" + fileName; FileInfo fi = new FileInfo(str_FilePath); if (fi.Exists == false){ return null; } StreamReader sr = File.OpenText(str_FilePath); if (sr != null){ string str_Data = sr.ReadToEnd(); sr.Close(); return str_Data; } else{ return null; } } static private string SerializeObject<T>(T obj) { string str_XmlizedString = null; MemoryStream ms = new MemoryStream(); XmlSerializer xs = new XmlSerializer(typeof(T)); XmlTextWriter xtw = new XmlTextWriter(ms, Encoding.UTF8); xs.Serialize(xtw, obj); ms = (MemoryStream)xtw.BaseStream; str_XmlizedString = UTF8ByteArrayToString(ms.ToArray()); return str_XmlizedString; } static private T DeserializeObject<T>(string str_XmlizedString) { XmlSerializer xs = new XmlSerializer(typeof(T)); MemoryStream ms = new MemoryStream(StringToUTF8ByteArray(str_XmlizedString)); return (T)xs.Deserialize(ms); } static private string UTF8ByteArrayToString(byte[] ba) { UTF8Encoding ue = new UTF8Encoding(); string str_Constructed = ue.GetString(ba); return (str_Constructed); } static private byte[] StringToUTF8ByteArray(string str_XmlString) { UTF8Encoding ue = new UTF8Encoding(); byte[] ba = ue.GetBytes(str_XmlString); return ba; } } //public class SmallGameData{ // public string name; //}
实例
using UnityEngine; using System.Collections; public class SmallGameData{ static private SmallGameData instence; static public SmallGameData Instence{ get{ if(instence==null){ instence=GameDataMgr.LoadData<SmallGameData>(SmallGameData.FileName,SmallGameData.FileName); if(instence==null){ instence=new SmallGameData(); } } return instence; } } private SmallGameData(){ } static private string filePath=Application.persistentDataPath; static public string FilePath {get {return filePath;}} static private string fileName="SmallGameData.xml"; static public string FileName {get {return fileName;}} private string name; public string Name {get {return name;}set {name = value;}} }
相关文章推荐
- iOS开发53-Github和Source Tree的使用
- 《软件分析和系统设计》读书笔记之一
- 复制文件
- C# 多个Main()方法
- Audio相关Log
- 关于viewController中的tableview的cell在pop回来后仍有选中效果的解决方法
- Light OJ 1377 - Blade and Sword (BFS)
- 王立平--Unity综上所述控制
- 将数据填充到已有的EXECL模板中
- 操作系统基础-死锁
- Linux下通过rm -f删除大量文件时提示"-bash: /bin/rm: Argument list too long"的解决方法
- 结构体&结构体指针&结构体指针数组
- PLL(锁相环)电路原理
- 部署虚拟后网卡不能启动问题
- 移动应用性能测试白皮书
- QQ登录网站接入功能实现--非官方文档搬运
- 8.15 练习
- Android使用AIDL传递对象
- Qt版Word字处理软件
- Android Push Notifications using Google Cloud Messaging (GCM), PHP and MySQL