I wrote sth.. hope to help u..
public class TAObjectSerializer
{
private static void __serializeData(object result, Type propType, XmlWriter wr)
{
if (result != null)
{
TypeConverter tc = TypeDescriptor.GetConverter(result);
if (tc != null && tc.CanConvertTo(typeof(string)) && tc.CanConvertFrom(typeof(string)))
{
wr.WriteString(tc.ConvertTo(result, typeof(string)) as string);
}
else if (propType.IsArray)
{
Array tmp = result as Array;
if (propType.GetElementType() == typeof(object))
{
for (int i = 0; i < tmp.Length; i++)
{
object v = tmp.GetValue(i);
wr.WriteStartElement("item");
if (v == null)
{
wr.WriteAttributeString("type", "");
}
else
{
Type vt = v.GetType();
wr.WriteAttributeString("type", (vt.IsPrimitive || v is string || v is decimal || v is DateTime || vt.IsArray) ? vt.ToString() : vt.AssemblyQualifiedName);
__serializeData(v, v.GetType(), wr);
}
wr.WriteEndElement();
}
}
else
{
for (int i = 0; i < tmp.Length; i++)
{
object v = tmp.GetValue(i);
wr.WriteStartElement("item");
if (v != null)
{
__serializeData(v, v.GetType(), wr);
}
wr.WriteEndElement();
}
}
}
else if (propType.IsSerializable)
{
using (MemoryStream __ = new MemoryStream())
{
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(__, result);
wr.WriteString(Convert.ToBase64String(__.ToArray()));
}
}
else if (propType.IsClass)
{
wr.WriteRaw(__serialize(result));
}
}
}
private static void __serializeItem(object obj, PropertyInfo pi, XmlWriter wr)
{
Type propType = pi.PropertyType;
object result = pi.GetValue(obj, null);
wr.WriteStartElement("property");
wr.WriteAttributeString("type", (propType.IsPrimitive || result is string || result is decimal || result is DateTime || propType.IsArray) ? propType.ToString() : propType.AssemblyQualifiedName);
wr.WriteAttributeString("name", pi.Name);
__serializeData(result, propType, wr);
wr.WriteEndElement();
}
private static string __serialize(object obj)
{
StringBuilder sb = new StringBuilder();
XmlWriterSettings set = new XmlWriterSettings();
set.OmitXmlDeclaration = true;
using (XmlWriter wr = XmlWriter.Create(sb, set))
{
Type t = obj.GetType();
wr.WriteStartElement("object");
wr.WriteAttributeString("type", t.AssemblyQualifiedName);
if (t.IsClass && !(obj is string))
{
PropertyInfo[] list = t.GetProperties();
foreach (PropertyInfo pi in list)
{
if (pi.CanRead && pi.CanWrite && pi.GetCustomAttributes(typeof(NonSerializedAttribute), true).Length == 0)
{
__serializeItem(obj, pi, wr);
}
}
}
wr.WriteEndElement();
}
return sb.ToString();
}
public static XmlDocument Serialize(object obj)
{
if (obj == null)
throw new ArgumentNullException("obj");
XmlDocument doc = new XmlDocument();
string str = __serialize(obj);
if (!string.IsNullOrEmpty(str))
doc.LoadXml(str);
return doc;
}
private static object __deserializeItem(Type propType, XmlNode node)
{
TypeConverter tc = TypeDescriptor.GetConverter(propType);
if (tc != null && tc.CanConvertTo(typeof(string)) && tc.CanConvertFrom(typeof(string)))
{
return tc.ConvertFrom(node.InnerText);
}
else if (propType.IsArray)
{
if (propType.GetElementType() == typeof(object))
{
XmlNodeList nl = node.SelectNodes("item");
Array tmp = Array.CreateInstance(typeof(object), nl.Count);
for (int i = 0; i < nl.Count; i++)
{
XmlNode p = nl[i];
Type _t = Type.GetType(p.Attributes["type"].Value);
if (_t == null)
tmp.SetValue(null, i);
else
tmp.SetValue(__deserializeItem(_t, p), i);
}
return tmp;
}
else
{
Type _t = propType.GetElementType();
XmlNodeList nl = node.SelectNodes("item");
Array tmp = Array.CreateInstance(_t, nl.Count);
for (int i = 0; i < nl.Count; i++)
{
XmlNode p = nl[i];
tmp.SetValue(__deserializeItem(_t, p), i);
}
return tmp;
}
}
else if (propType.IsSerializable)
{
using (MemoryStream __ = new MemoryStream(Convert.FromBase64String(node.InnerText)))
{
BinaryFormatter bf = new BinaryFormatter();
return bf.Deserialize(__);
}
}
else if (propType.IsClass)
{
return __deserialize(node);
}
return null;
}
private static object __deserialize(XmlNode t)
{
try
{
object tmp = Activator.CreateInstance(Type.GetType(t.Attributes["type"].Value));
XmlNodeList nl = t.SelectNodes("property");
Type objType = tmp.GetType();
foreach (XmlNode p in nl)
{
string name = p.Attributes["name"].Value;
PropertyInfo pi = objType.GetProperty(name);
Type propType = Type.GetType(p.Attributes["type"].Value);
if (propType == pi.PropertyType)
{
pi.SetValue(tmp, __deserializeItem(propType, p), null);
}
}
return tmp;
}
catch
{
}
return null;
}
public static object Deserialize(XmlDocument doc)
{
XmlNode nd = doc.SelectSingleNode("object");
if (nd == null)
throw new ArgumentOutOfRangeException();
return __deserialize(nd);
}
}
**
Usage :
Serialize : TAObjectSerializer.Serialize(myClassInstance); //this returns as XmlDocument
Deserialize : TAObjectSerializer.Deserialize(myXmlDocument); //this returns as object instance
**
this class will serialize all READ_WRITE properties if property is not marked by NonSerializedAttribute attribute
Good luck!