1. 程式人生 > >再弄一片文章湊個4篇文章的數,主要是用於XML和 Binary序列化和反序列化的類

再弄一片文章湊個4篇文章的數,主要是用於XML和 Binary序列化和反序列化的類

using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Xml.Serialization;

namespace NOAS.PublicOpinionMonitor.Common
{
    public class SerializeUtility
    {
        public enum SerializeType
        {
            XML,
            Binary
        }

        #region 序列化

        /// <summary>
        /// 序列化
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <param name="savePath"></param>
        /// <param name="type"></param>
        public static void Serialize<T>(T t, string savePath, SerializeType type)
        {
            switch (type)
            {
                case SerializeType.XML:
                    SerializeToXML<T>(t, savePath);
                    break;
                case SerializeType.Binary:
                    SerializeToBinary<T>(t, savePath);
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 序列化為XML格式檔案
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <param name="savePath"></param>
        private static void SerializeToXML<T>(T t, string savePath)
        {
            if (!Directory.GetParent(savePath).Exists)
                Directory.GetParent(savePath).Create();

            XmlSerializer ser = new XmlSerializer(t.GetType());
            if (File.Exists(savePath))
            {
                File.Delete(savePath);
            }
            using (FileStream stream = new FileStream(savePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
            {
                ser.Serialize(stream, t);
            }
        }

        /// <summary>
        /// 序列化為Binary格式檔案
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <param name="savePath"></param>
        private static void SerializeToBinary<T>(T t, string savePath)
        {
            if (!Directory.GetParent(savePath).Exists)
                Directory.GetParent(savePath).Create();

            BinaryFormatter serilazer = new BinaryFormatter();
            using (FileStream stream = new FileStream(savePath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
            {
                serilazer.Serialize(stream, t);
            }
        }
        #endregion


        #region 反序列化

        public static T DeSerialize<T>(string readPath, SerializeType type)
        {
            switch (type)
            {
                case SerializeType.XML:
                    return DeSerializeXML<T>(readPath);
                case SerializeType.Binary:
                    return DeSerializeBinary<T>(readPath);
                default:
                    return default(T);
            }
        }

        /// <summary>
        /// 反序列化XML檔案
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="savePath"></param>
        /// <returns></returns>
        private static T DeSerializeXML<T>(string readPath)
        {
            if (!File.Exists(readPath)) 
            {
                return default(T);
            }
            XmlSerializer ser = new XmlSerializer(typeof(T));
            using (FileStream stream = new FileStream(readPath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
            {
                return (T)ser.Deserialize(stream);
            }
        }

        /// <summary>
        /// 反序列化Binary檔案
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="savePath"></param>
        /// <returns></returns>
        private static T DeSerializeBinary<T>(string readPath)
        {
            BinaryFormatter serilazer = new BinaryFormatter();
            using (FileStream stream = new FileStream(readPath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
            {
                return (T)serilazer.Deserialize(stream);
            }
        }
        #endregion
    }
}

這個方式比較的就是,直接讀取檔案的方式。