unity xml序列化与反序列化 多平台

换平台确实是一个头疼的问题,本来在pc用.net的json处理数据很是顺手的,但是发布web版本后,发现他不支持。后面找了好几个开源json都不能很好的支持web,或者不能支持List等。于是我就想着自己利用xml写一个序列化与反序列化用来存储数据或者解析数据。这里我只测试了pc与web平台,移动端的没有测试。


自定义xml序列化脚本,因为可能使用到中文,所以设置utf-8编码:

using UnityEngine;
using System.Collections;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System;

/// <summary>
/// XML序列化
/// </summary>
public class CustomXMLData  {

    private static CustomXMLData xmlData;
    /// <summary>
    /// 获取本类对象
    /// </summary>
    /// <returns></returns>
    public static CustomXMLData getInstance()
    {
        if (xmlData == null)
        {
            xmlData = new CustomXMLData();
        }
        return xmlData;
    }

    /// <summary>
    /// 序列化--内存流
    /// </summary>
    /// <param name="pObject">对象</param>
    /// <param name="t">类型</param>
    /// <returns></returns>
    public string SerializeObject(object pObject,Type t)
    {
        string XmlizedString = null;
        MemoryStream memoryStream = new MemoryStream();
        XmlSerializer xs = new XmlSerializer(t);
        XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream,Encoding.UTF8);
        xs.Serialize(xmlTextWriter,pObject);
        memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
        XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());
        Debug.Log("" + XmlizedString);
        return XmlizedString;

    }

    /// <summary>
    /// 字节转string
    /// </summary>
    /// <param name="characters">字节数组</param>
    /// <returns></returns>
    private string UTF8ByteArrayToString(byte[] characters)
    {
        UTF8Encoding encoding = new UTF8Encoding();
        string constructedString = encoding.GetString(characters);
        return (constructedString);
    }

    /// <summary>
    /// 反序列化
    /// </summary>
    /// <param name="pXmlizedString">string内容</param>
    /// <param name="t">类型</param>
    /// <returns></returns>
    public object DeserializeObject(string pXmlizedString,Type t)
    {
        XmlSerializer xs = new XmlSerializer(t);
        MemoryStream memoryStream = new MemoryStream(StringToUTF8ByteArray(pXmlizedString));
        XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream,Encoding.UTF8);
        return xs.Deserialize(memoryStream);
    }

    /// <summary>
    /// 字符串转字节数组
    /// </summary>
    /// <param name="pXmlString">字符内容</param>
    /// <returns></returns>
    private byte[] StringToUTF8ByteArray(string pXmlString)
    {
        UTF8Encoding encoding = new UTF8Encoding();
        byte[] byteArray = encoding.GetBytes(pXmlString);
        return byteArray;
    }

    /// <summary>
    /// 序列化--XML文件
    /// </summary>
    /// <param name="pObject">对象</param>
    /// <param name="t">类型</param>
    /// <param name="XMLPath">生成的xml路径</param>
    public void SerializeObjectXML(object pObject,Type t,string XMLPath)
    {
        XmlWriterSettings ws = new XmlWriterSettings();
        ws.Encoding = Encoding.UTF8;
        XmlWriter xmTextWriter = XmlWriter.Create(XMLPath,ws);
        XmlSerializer xmlFormat = new XmlSerializer(t);
        xmlFormat.Serialize(xmTextWriter,pObject);
    }
}

测试脚本:

using UnityEngine;
using System.Collections;

public class Test : MonoBehavIoUr {

	// Use this for initialization
	void Start () {
        //定义Player对象
        Player player = new Player("学号ID",50,26.5f);

        //序列化 通过内存流转为字符串,可以将字符串存于数据库中存储操作或其他操作
        string serStr=CustomXMLData.getInstance().SerializeObject(player,typeof(Player));
        //反序列化 将字符串解析为Player对象
        Player tempPlayer = (Player)CustomXMLData.getInstance().DeserializeObject(serStr,typeof(Player));
        
        //序列化 创建本地xml文件
        CustomXMLData.getInstance().SerializeObjectXML(player,typeof(Player),Application.dataPath + "/1.xml");
        //读取xml的内容 并反序列化
        StartCoroutine(ReadXml(Application.dataPath + "/1.xml"));
	}
	
	// Update is called once per frame
	void Update () {
	
	}

    /// <summary>
    /// 读取xml的内容 并反序列化
    /// </summary>
    /// <param name="xmlPath">xml文件的路径</param>
    /// <returns></returns>
    IEnumerator ReadXml(string xmlPath)
    {
        Player tempPlayer=null;
        WWW w = new WWW(xmlPath);
        yield return w;
        if(w.error==null)
            tempPlayer = (Player)CustomXMLData.getInstance().DeserializeObject(w.text,typeof(Player));
    }

}

相关文章

引言 NOKIA 有句著名的广告语:“科技以人为本”。任何技术都是为了满足人的生产生活需要而产生的。具体...
Writer:BYSocket(泥沙砖瓦浆木匠) 微博:BYSocket 豆瓣:BYSocket Reprint it anywhere u want. 文章...
Writer:BYSocket(泥沙砖瓦浆木匠) 微博:BYSocket 豆瓣:BYSocket Reprint it anywhere u want. 文章...
http://blog.jobbole.com/79252/ 引言 NOKIA 有句著名的广告语:“科技以人为本”。任何技术都是为了满...
(点击上方公众号,可快速关注) 公众号:smart_android 作者:耿广龙|loonggg 点击“阅读原文”,可查看...
一、xml与xslt 相信所有人对xml都不陌生,其被广泛的应用于数据数据传输、保存与序列化中,是一种极为强...