Does anyone have an implementation for object serialization that support collections, such as ICollection, IEnumerable, IList, IDictionary?
Below is my implementation, and it’s not so clean.
[Serializable]
public abstract class IXmlable
{
public virtual string ToXml()
{
Type type = GetType();
PropertyInfo[] properties = type.GetProperties();
string ret = "<" + type.Name + ">";
foreach (PropertyInfo item in properties)
{
object value = item.GetValue(this, null);
if (value is IConvertible)
ret += ("<" + item.Name + ">" + (value as IConvertible).ToString(CultureInfo.GetCultureInfo("en-US")) + "</" + item.Name + ">").Replace("&", "&");
else
ret += ("<" + item.Name + ">" + (value != null ? value.ToString() : String.Empty) + "</" + item.Name + ">").Replace("&", "&");
}
ret += "</" + type.Name + ">";
return ret;
}
/// <summary>
/// A class for serializing objects
/// </summary>
public static class Serializer
{
public static string ToXML<T>(this List<T> parameter)
{
return SerializeObject(parameter);
}
/// <summary>
/// This function serialize object.
/// </summary>
/// <param name="parameters">The object to be serialize</param>
/// <returns>Returns the XML presentation of serialized object</returns>
public static string SerializeObject(params object[] parameters)
{
var doc = new XDocument(new XElement("root"));
string swString = string.Empty;
foreach (var parameter in parameters)
{
var list = parameter as IEnumerable;
IXmlable xmlable;
if (list != null)
{
Type type = list.GetType().GetGenericArguments()[0];
if (type.BaseType != null && type.BaseType.Name == "IXmlable")
{
string typeName = type.Name;
swString = "<?xml version=\"1.0\"?><ArrayOf" + typeName + " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">";
swString = list.Cast<IXmlable>().Aggregate(swString, (current, item) => current + item.ToXml());
swString += "</ArrayOf" + typeName + ">";
}
}
else if ((xmlable = parameter as IXmlable) != null)
{
swString = xmlable.ToXml();
}
else
{
swString = Serialize(parameter);
}
doc.Root.Add(XDocument.Parse(swString).Root);
}
return doc.ToString();
}
/// <summary>
/// Serializes the specified parameter.
/// </summary>
/// <param name="parameter">The parameter.</param>
/// <returns></returns>
private static string Serialize(object parameter)
{
using (var sw = new MemoryStream())
{
new XmlSerializer(parameter.GetType()).Serialize(sw, parameter);
sw.Position = 0;
var buffer = new byte[sw.Length];
sw.Read(buffer, 0, (int)sw.Length);
return Encoding.Default.GetString(buffer);
}
}
}