Yesterday was JSON serialization, today is XML serialization.
I've taken some of the suggestions from there and made modifications, as well as making other modifications that were not suggested but should be made.
/// <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;
}
}
}
The idea is to keep it as simple as possible, and provide a common, natural form of accessing serialization.
A couple of changes:
- I added the exception that is thrown when
value
isnull
. This wasn't there until Heslacher mentioned that I only had one spot that exceptions were thrown, as I had forgot to put it on that check as well. throwErrors
and all related messages/comments/documentation have been refactored tothrowExceptions
,errors
toexceptions
, et al.