码迷,mamicode.com
首页 > 其他好文 > 详细

[Unity 笔记] unity中如何将Object序列化成xml字符串并保存

时间:2014-09-27 00:55:49      阅读:507      评论:0      收藏:0      [点我收藏+]

标签:unity3d   xml序列化   


需要使用到以下这几个c#内置的命名空间。

using System.Xml;
using System.IO;
using System.Xml.Serialization;


序列化并存储xml文件

            XmlTextWriter xWrite = new XmlTextWriter(filename, null);
            XmlSerializer sl = new XmlSerializer(type);
            sl.Serialize(xWrite, target);
            xWrite.Close();


读取xml文件并反序列化

            XmlReader xRead = new XmlTextReader(filename);
            XmlSerializer sl = new XmlSerializer(type);
            object obj = sl.Deserialize(xRead);
            xRead.Close();
            return obj;


完整源码如下:

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

namespace CharacterBase
{
    public class XmlIO
    {
        public static object LoadFromXml(System.Type type, string filename)
        {
            XmlReader xRead = new XmlTextReader(filename);
            XmlSerializer sl = new XmlSerializer(type);
            object obj = sl.Deserialize(xRead);
            xRead.Close();
            return obj;
        }

        public static void SaveToXml(string filename, System.Type type, object target)
        {
            XmlTextWriter xWrite = new XmlTextWriter(filename, null);
            XmlSerializer sl = new XmlSerializer(type);
            sl.Serialize(xWrite, target);
            xWrite.Close();
        }
    }
}



[Unity 笔记] unity中如何将Object序列化成xml字符串并保存

标签:unity3d   xml序列化   

原文地址:http://blog.csdn.net/hunter_hb/article/details/39586309

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!