加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

对象序列化成XML存储,XML反序列换成对象

发布时间:2020-12-16 08:48:22 所属栏目:百科 来源:网络整理
导读: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 LoadDataT(string name,string path){T result=default(T);string str
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;}}
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读