Use this universal mini snippet to log your objects as serialized strings:
public static string SerializeToString(T obj)
{
StringWriter sw = new StringWriter();
XmlSerializer serializer = new XmlSerializer(typeof(T));
serializer.Serialize(sw, obj);
return sw.ToString();
}
If you have problems when doing Serialization use this simple function:
public static string ObjectToString(object obj)
{
Type type = obj.GetType();
PropertyInfo[] props = type.GetProperties();
StringBuilder sb = new StringBuilder();
foreach (var prop in props)
{
sb.Append(prop.Name);
sb.Append("=");
sb.Append(prop.GetValue(obj, null));
sb.Append(Environment.NewLine);
}
return sb.ToString();
}
