• 再弄一片文章凑个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
        }
    }
    


    这个方式比较的就是,直接读取文件的方式。

  • 相关阅读:
    递归获取指定盘符下的所有文件及文件夹
    单例模式和多线程有没有关系?
    eclipse启动tomcat时设置端口
    dozer转化对象
    枚举
    dubbo
    json
    配网失败问题
    esp_err_t esp_event_loop_init(system_event_cb_t cb, void *ctx);
    base64编码
  • 原文地址:https://www.cnblogs.com/dingdingmao/p/3146455.html
Copyright © 2020-2023  润新知