• 分享一个对象序列化为XML和反序列化的扩展方法


        public static class XmlSerializerExtensions
        {
            
    #region Private fields
            
    private static readonly Dictionary<RuntimeTypeHandle, XmlSerializer> ms_serializers = new Dictionary<RuntimeTypeHandle, XmlSerializer>();
            
    #endregion

            
    #region Public methods
            
    /// <summary>
            
    /// 序列化对象为xml字符串
            
    /// </summary>
            
    /// <typeparam name = "T"></typeparam>
            
    /// <param name = "value"></param>
            
    /// <returns></returns>
            public static string ToXml<T>(this T value)
                
    where T : new()
            {
                var _serializer 
    = GetValue(typeof(T));
                
    using (var _stream = new MemoryStream())
                {
                    
    using (var _writer = new XmlTextWriter(_stream, new UTF8Encoding()))
                    {
                        _serializer.Serialize(_writer, value);
                        
    return Encoding.UTF8.GetString(_stream.ToArray());
                    }
                }
            }

            
    /// <summary>
            
    /// 反序列化xml字符串为对象
            
    /// </summary>
            
    /// <typeparam name = "T">要序列化成何种对象</typeparam>
            
    /// <param name = "srcString">xml字符串</param>
            
    /// <returns></returns>
            public static T FromXml<T>(this string srcString)
                
    where T : new()
            {
                var _serializer 
    = GetValue(typeof(T));
                
    using (var _stringReader = new StringReader(srcString))
                {
                    
    using (XmlReader _reader = new XmlTextReader(_stringReader))
                    {
                        
    return (T)_serializer.Deserialize(_reader);
                    }
                }
            }
            
    #endregion

            
    #region Private methods
            
    private static XmlSerializer GetValue(Type type)
            {
                XmlSerializer _serializer;
                
    if (!ms_serializers.TryGetValue(type.TypeHandle, out _serializer))
                {
                    
    lock (ms_serializers)
                    {
                        
    if (!ms_serializers.TryGetValue(type.TypeHandle, out _serializer))
                        {
                            _serializer 
    = new XmlSerializer(type);
                            ms_serializers.Add(type.TypeHandle, _serializer);
                        }
                    }
                }
                
    return _serializer;
            }
            
    #endregion
        }
  • 相关阅读:
    codevs 3115 高精度练习之减法 swap
    codevs 3116 高精度练习之加法
    poj 3984 迷宫问题
    codevs m进制转化成10进制
    codevs 1214 线段覆盖
    codevs 3143 二叉树的序遍历
    codevs 3145 汉诺塔
    HDU 5093 Battle ships [二分图匹配]
    HDU 5074 Hatsune Miku [dp] ——2014鞍山现场赛E题
    ZOJ 3793 First Digit (逗比题)
  • 原文地址:https://www.cnblogs.com/leeolevis/p/1911579.html
Copyright © 2020-2023  润新知