pc与web平台利用xml写一个序列化与反序列化用来存储数据

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

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

  using Engine;

  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);

  }

  }

  测试脚本:

  [csharp] view plain copy

  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));

  }

  }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值