To do it I suggest you to read XML Serializer and Deserializer Tutorials.
Hope helps. Greetings!
UPDATE:
To serialize a unknown object you must pass all object a add to the serializer to say hwo the must serialize:
public static void Save<T>(T item, string filename, IEnumerable<Type> typeList) where T : class, new()
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T), typeList.ToArray());
// To write to a file, create a StreamWriter object.
StreamWriter writer = null;
try
{
writer = new StreamWriter(filename);
var ns = new XmlSerializerNamespaces();
ns.Add(string.Empty, string.Empty);
xmlSerializer.Serialize(writer, item, ns);
}
catch (Exception ex)
{
}
finally
{
if (writer != null)
writer.Close();
}
}
To Deserialize unknown object its the same:
public static T Load<T>(string filename, IEnumerable<Type> typeList) where T : class, new()
{
if (!File.Exists(filename))
return new T();
TextReader fileStream = null;
try
{
// Construct an instance of the XmlSerializer with the type
// of object that is being deserialized.
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T), typeList.ToArray());
// To read the file, create a FileStream.
fileStream = new StreamReader(filename);
return xmlSerializer.Deserialize(fileStream) as T;
// Call the Deserialize method and cast to the object type.
// return xmlSerializer.Deserialize(fileStream) as T;
}
catch (Exception ex)
{
return new T();
}
finally
{
if (fileStream != null)
fileStream.Close();
}
}
UPDATE2:
T -> is the object you want to serialize.
filname -> the path/destinaton where you whant tu save it.
IEnumerable -> is a list of Types. Only declare a IEnumerable and Add all you type you will serialize there and after pass it on the Save, Load method.