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

前端之家收集整理的这篇文章主要介绍了对象序列化成XML存储,XML反序列换成对象前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
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;}}
}

猜你在找的XML相关文章