You can iterate object's properties through reflection, compare it's properties with 'fresh' instance, and write down the difference somehow. But you should address many problems if you choose that path, like null
handling, serializing non-serializable types, serializing references, etc. Here's just a sketch:
public static string ChangedPropertiesToXml<T>(T changedObj)
{
XmlDocument doc=new XmlDocument();
XmlNode typeNode = doc.CreateNode(XmlNodeType.Element, typeof (T).Name, "");
doc.AppendChild(typeNode);
T templateObj = Activator.CreateInstance<T>();
foreach (PropertyInfo info in
typeof (T).GetProperties(BindingFlags.Instance | BindingFlags.Public))
{
if (info.CanRead && info.CanWrite)
{
object templateValue = info.GetValue(templateObj, null);
object changedValue = info.GetValue(changedObj, null);
if (templateValue != null && changedValue != null && !templateValue.Equals(changedValue))
{
XmlElement elem = doc.CreateElement(info.Name);
elem.InnerText = changedValue.ToString();
typeNode.AppendChild(elem);
}
}
}
StringWriter sw=new StringWriter();
doc.WriteContentTo(new XmlTextWriter(sw));
return sw.ToString();
}
A call:
Button b = new Button();
b.Name = "ChangedName";
Console.WriteLine(SaveChangedProperties(b));
An output:
<Button>
<Name>ChangedName</Name>
</Button>