Serialize a Class to XML

Sometimes it helps to serialize an object and include it into the log file in a QA build for posterity. Inherit from this class and you can use Enterprise Logger to log basic meaningful types to the log file.

using System.IO;
using System.Xml;

public class Serializer
{
    public override string ToString()
    {
        Serialization.XmlSerializer xmlSerializer = new Serialization.XmlSerializer(this.GetType());

        using (StringWriter TextWriter = new StringWriter()) {
            xmlSerializer.Serialize(TextWriter, this);
            return TextWriter.ToString();
        }
    }
}
using System.Runtime.Serialization;
using System.Collections.ObjectModel;

[Serializable()]
public class UserRole : Serializer
{
    public UserRole()
    {
    }
}

Now write the object using Enterprise Library Logging Framework, where role is in instance of UserRole

Logger.Write(role);