Return to Snippet

Revision: 17163
at August 25, 2009 17:52 by rengber

Initial Code
public static string ObjectToString(object o)
  XDocument doc = new XDocument();
  string objectType = o.GetType().ToString();
  doc.AddFirst(new XElement(objectType));

  foreach (PropertyInfo pi in o.GetType().GetProperties())
    doc.Root.Add(new XElement(pi.Name, Convert.ToString(pi.GetValue(o, null))));
  return doc.ToString();

Initial URL

Initial Description
Useful in cases where the object author has specified their own serialization that doesn't work for you.

Initial Title
Dump an Arbitrary Object to XML (Bypassing Default Serialization)

Initial Tags
xml, c

Initial Language