Skip to content

Instantly share code, notes, and snippets.

@longtth
Created April 12, 2018 08:58
Show Gist options
  • Save longtth/3813561f7d20d1ed6830146a19298a0b to your computer and use it in GitHub Desktop.
Save longtth/3813561f7d20d1ed6830146a19298a0b to your computer and use it in GitHub Desktop.
/// <summary>
/// Provides methods for Serialization and Deserialization of XML/Extensible Markup Language documents.
/// </summary>
public class XmlSerialization
{
/// <summary>
/// Serializes an object to an XML/Extensible Markup Language string.
/// </summary>
/// <typeparam name="T">The type of the object to serialize.</typeparam>
/// <param name="value">The object to serialize.</param>
/// <param name="serializedXml">Filled with a string that is the XmlSerialized object.</param>
/// <param name="throwExceptions">If true, will throw exceptions. Otherwise, returns false on failures.</param>
/// <returns>A boolean value indicating success.</returns>
public static bool Serialize<T>(T value, ref string serializedXml, bool throwExceptions = false)
{
#if DEBUG
#warning When in DEBUG Mode XML Serialization Exceptions will be thrown regardless of throwExceptions paramter.
throwExceptions = true;
#endif
if (value == null)
if (throwExceptions)
throw new ArgumentNullException("The value is expected to be a non-null object.");
else
return false;
try
{
XmlSerializer xmlserializer = new XmlSerializer(typeof(T));
using (StringWriter stringWriter = new StringWriter())
using (XmlWriter writer = XmlWriter.Create(stringWriter))
{
xmlserializer.Serialize(writer, value);
serializedXml = stringWriter.ToString();
return true;
}
}
catch
{
if (throwExceptions)
throw;
return false;
}
}
/// <summary>
/// Deserializes an XML/Extensible Markup Language string to an object.
/// </summary>
/// <typeparam name="T">The type of the object to serialize.</typeparam>
/// <param name="value">The XML string representing the serialized object.</param>
/// <param name="deserializedObject">Filled with the object that is the XmlDeserialized string.</param>
/// <param name="throwExceptions">If true, will throw exceptions. Otherwise, returns false on failures.</param>
/// <returns>A boolean value indicating success.</returns>
public static bool Deserialize<T>(string value, ref T deserializedObject, bool throwExceptions = false)
{
#if DEBUG
#warning When in DEBUG Mode XML Deserialization Exceptions will be thrown regardless of throwExceptions paramter.
throwExceptions = true;
#endif
if (value == null)
if (throwExceptions)
throw new ArgumentNullException("The value is expected to be a non-null object.");
else
return false;
try
{
XmlSerializer xmlserializer = new XmlSerializer(typeof(T));
using (StringReader stringReader = new StringReader(value))
using (XmlReader reader = XmlReader.Create(stringReader))
{
if (!xmlserializer.CanDeserialize(reader))
if (throwExceptions)
throw new ArgumentException("The provided value cannot be deserialized to the specified type.");
else
return false;
deserializedObject = (T)(xmlserializer.Deserialize(reader));
return true;
}
}
catch
{
if (throwExceptions)
throw;
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment