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