程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 再弄一片文章湊個4篇文章的數,主要是用於XML和 Binary序列化和反序列化的類

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

編輯:C#入門知識

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
    }
}


這個方式比較的就是,直接讀取文件的方式。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved