游戏蛮牛学习群(纯技术交流,不闲聊):159852603
游戏蛮牛 手机端
开启辅助访问
 找回密码
 注册帐号

扫一扫,访问微社区

首页   >   博客   >   碎碎

xml序列化和反序列化

个人分类: 存档和读档 | 2018-10-8 10:43

具体运用的时候,只需选择其一即可,不需要写入全部方法。

[code]csharpcode:

using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using UnityEngine;
/// <summary>
/// Xml序列化与反序列化
/// 2018.9.27
/// </summary>
public class XmlUtil
{
    #region 反序列化
    /// <summary>
    /// 反序列化
    /// </summary>
    /// <param name="type">类型</param>
    /// <param name="xml">XML字符串</param>
    /// <returns></returns>
    public static object Deserialize(Type type, string xml)
    {
        try
        {
            using (StringReader sr = new StringReader(xml))
            {
                XmlSerializer xmldes = new XmlSerializer(type);
                return xmldes.Deserialize(sr);
            }
        }
        catch (Exception e)
        {
            return null;
        }
    }
    public static T DeserializationFile<T>(string path)
    {
        T t = default(T);
        try
        {
            StreamReader reader = new StreamReader(path);
            XmlSerializer mySerializer = new XmlSerializer(typeof(T));
            t = (T)mySerializer.Deserialize(reader);
            reader.Close();
            return t;
        }
        catch(Exception e)
        {
            Debug.LogError("XmlSerializerException");
            return t;
        }
    }
    public static object Deserialize(Type type, Stream stream)
    {
        XmlSerializer xmldes = new XmlSerializer(type);
        return xmldes.Deserialize(stream);
    }
    public static object LoadFromXml(Type type, string fileName)
    {
        XmlSerializer xs = new XmlSerializer(type);
        XmlReader xr = new XmlTextReader(fileName);
        object obj = xs.Deserialize(xr);
        xr.Close();
        return obj;
    }
    public static object Deserialize(string xmlString, Type type)
    {
        MemoryStream ms = new MemoryStream(StringToUTF8ByteArray(xmlString));
        XmlSerializer xs = new XmlSerializer(type);
        return xs.Deserialize(ms);
    }
    #endregion

    #region 序列化
    /// <summary>
    /// 序列化
    /// </summary>
    /// <param name="type">类型</param>
    /// <param name="obj">对象</param>
    /// <returns></returns>
    public static string Serializer(Type type, object obj)
    {
        MemoryStream Stream = new MemoryStream();
        XmlSerializer xml = new XmlSerializer(type);
        try
        {
            //序列化对象
            xml.Serialize(Stream, obj);
        }
        catch (InvalidOperationException)
        {
            throw;
        }
        Stream.Position = 0;
        StreamReader sr = new StreamReader(Stream);
        string str = sr.ReadToEnd();

        sr.Dispose();
        Stream.Dispose();

        return str;
    }
    public static void SaveToXml(string fileName, Type type, object obj)
    {
        //TextWriter writer = new StreamWriter(fileName);
        XmlWriter xw = new XmlTextWriter(fileName, Encoding.UTF8);
        XmlSerializer xs = new XmlSerializer(type);
        xs.Serialize(xw, obj);
        xw.Close();
    }
    public string SerializeObject(object pObject, System.Type ty)
    {
        string XmlizedString = null;
        MemoryStream memoryStream = new MemoryStream();
        XmlSerializer xs = new XmlSerializer(ty);
        XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
        xs.Serialize(xmlTextWriter, pObject);
        memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
        XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());
        return XmlizedString;
    }
    #endregion

    /// <summary>
    /// 字节数组转string
    /// </summary>
    /// <param name="b">字节数组</param>
    /// <returns></returns>
    public static string UTF8ByteArrayToString(byte[] b)
    {
        UTF8Encoding encoding = new UTF8Encoding();
        string s = encoding.GetString(b);
        return s;
    }
    /// <summary>
    /// 字符串转字节数组
    /// </summary>
    /// <param name="s">字符串</param>
    /// <returns></returns>
    public static byte[] StringToUTF8ByteArray(string s)
    {
        UTF8Encoding encoding = new UTF8Encoding();
        byte[] b = encoding.GetBytes(s);
        return b;
    }
}

0 0

作者的其他最新博客

评论 (0 个评论)

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 注册帐号

个人分类

标签

阅读排行

评论排行

推荐博客

最新博客

返回顶部