再弄一片文章凑个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,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,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,FileShare.ReadWrite))
            {
                return (T)serilazer.Deserialize(stream);
            }
        }
        #endregion
    }
}


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

相关文章

引言 NOKIA 有句著名的广告语:“科技以人为本”。任何技术都是为了满足人的生产生活需要而产生的。具体...
Writer:BYSocket(泥沙砖瓦浆木匠) 微博:BYSocket 豆瓣:BYSocket Reprint it anywhere u want. 文章...
Writer:BYSocket(泥沙砖瓦浆木匠) 微博:BYSocket 豆瓣:BYSocket Reprint it anywhere u want. 文章...
http://blog.jobbole.com/79252/ 引言 NOKIA 有句著名的广告语:“科技以人为本”。任何技术都是为了满...
(点击上方公众号,可快速关注) 公众号:smart_android 作者:耿广龙|loonggg 点击“阅读原文”,可查看...
一、xml与xslt 相信所有人对xml都不陌生,其被广泛的应用于数据数据传输、保存与序列化中,是一种极为强...