Last active
February 8, 2016 15:01
-
-
Save malteb247/3f533727608a230b63ed to your computer and use it in GitHub Desktop.
XmlExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class XmlExtensions | |
{ | |
/// <summary> | |
/// Cache serialization assemblies | |
/// </summary> | |
private static readonly Dictionary<RuntimeTypeHandle, XmlSerializer> serializer_cache = new Dictionary<RuntimeTypeHandle, XmlSerializer>(); | |
/// <summary> | |
/// used to remove obsolete xsd: and xsi: namespaces. | |
/// </summary> | |
private static readonly XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); | |
static XmlExtensions() | |
{ | |
ns.Add("", ""); | |
} | |
/// <summary> | |
/// Serialize T to xml | |
/// </summary> | |
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2202:Do not dispose objects multiple times", Justification = "Dispose is called one time, anyway calling it multiple times is safe, according to MSDN")] | |
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, ns); | |
return Encoding.UTF8.GetString(_stream.ToArray()); | |
} | |
} | |
} | |
/// <summary> | |
/// Serialize T to xml | |
/// </summary> | |
public static void ToXml<T>(this T value, Stream stream) | |
where T : new() | |
{ | |
var _serializer = GetValue(typeof(T)); | |
_serializer.Serialize(stream, value, ns); | |
} | |
/// <summary> | |
/// Deserialize xml string to T | |
/// </summary> | |
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2202:Do not dispose objects multiple times", Justification = "Dispose is called one time, anyway calling it multiple times is safe, according to MSDN")] | |
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); | |
} | |
} | |
} | |
/// <summary> | |
/// Deserialize xml string to T | |
/// </summary> | |
public static T FromXml<T>(this Stream source) | |
where T : new() | |
{ | |
var _serializer = GetValue(typeof(T)); | |
return (T)_serializer.Deserialize(source); | |
} | |
/// <summary> | |
/// Convert string to instance of <see cref="XmlElement"/> | |
/// </summary> | |
public static XmlElement ToXmlElement(this string s) | |
{ | |
XmlDocument doc = new XmlDocument(); | |
doc.LoadXml(s); | |
return doc.DocumentElement; | |
} | |
/// <summary> | |
/// Handle creation of xml serializers | |
/// </summary> | |
private static XmlSerializer GetValue(Type type) | |
{ | |
XmlSerializer serializer; | |
if (!serializer_cache.TryGetValue(type.TypeHandle, out serializer)) | |
{ | |
lock (serializer_cache) | |
{ | |
if (!serializer_cache.TryGetValue(type.TypeHandle, out serializer)) | |
{ | |
serializer = new XmlSerializer(type); | |
serializer_cache.Add(type.TypeHandle, serializer); | |
} | |
} | |
} | |
return serializer; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment