HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

XML serialization helper class

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
helperserializationclassxml

Problem

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.

```
///
/// Provides methods for Serialization and Deserialization of XML/Extensible Markup Language documents.
///
public class XmlSerialization
{
///
/// Serializes an object to an XML/Extensible Markup Language string.
///
/// The type of the object to serialize.
/// The object to serialize.
/// Filled with a string that is the XmlSerialized object.
/// If true, will throw exceptions. Otherwise, returns false on failures.
/// A boolean value indicating success.
public static bool Serialize(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;
}
}

///
/// Deserializes an XML/Extensible Markup Language string to an object.
///
//

Solution

if (!xmlserializer.CanDeserialize(reader))
                        if (throwExceptions)
                            throw new ArgumentException("The provided value cannot be deserialized to the specified type.");
                        else
                            return false;


I did tell you about braces {} didn't I ? Ok, I see you don't want to use them so don't whine if something goes terribly wrong.

So leaving that aside, lets talk about this snippet of code.

Because this is enclosed in a try..catch block which does check for throwExceptions there is no need to do it twice.

This can be simplified to (with braces ;-) )

if (!xmlserializer.CanDeserialize(reader))
{
    throw new ArgumentException("The provided value cannot be deserialized to the specified type.");
}


You are using using statments for objects which implements IDisposable which is the preferred way

There isn't anything more to say but this methods are well structured, readable and also maintainable.

Code Snippets

if (!xmlserializer.CanDeserialize(reader))
                        if (throwExceptions)
                            throw new ArgumentException("The provided value cannot be deserialized to the specified type.");
                        else
                            return false;
if (!xmlserializer.CanDeserialize(reader))
{
    throw new ArgumentException("The provided value cannot be deserialized to the specified type.");
}

Context

StackExchange Code Review Q#100930, answer score: 5

Revisions (0)

No revisions yet.