You can use a class that hold the data you won to export (and import) to xml.
You use the [Serializable]
for make the class available for xml export. So for example if you have this class:
[Serializable]
public class MyClassThatKeepTheData
{
public List<int> cListWithValues;
public int Value1;
public int Value2;
}
Then you can use the XmlSerializer to convert it to XML (and vice verse) as:
public static string ObjectToXML(Type type, object obby)
{
XmlSerializer ser = new XmlSerializer(type);
using (System.IO.MemoryStream stm = new System.IO.MemoryStream())
{
//serialize to a memory stream
ser.Serialize(stm, obby);
//reset to beginning so we can read it.
stm.Position = 0;
//Convert a string.
using (System.IO.StreamReader stmReader = new System.IO.StreamReader(stm))
{
string xmlData = stmReader.ReadToEnd();
return xmlData;
}
}
}
public static object XmlToObject(Type type, string xml)
{
object oOut = null;
if (xml != null && xml.Length > 0)
{
System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(type);
using (System.IO.StringReader sReader = new System.IO.StringReader(xml))
{
oOut = serializer.Deserialize(sReader);
sReader.Close();
}
}
return oOut;
}
and make the convert to XML as:
MyClassThatKeepTheData cTheObject = new MyClassThatKeepTheData();
ObjectToXML(typeof(MyClassThatKeepTheData), cTheObject)
Relative:
How to optimize class for viewstate
and I suggest also the protobuf-net is not XML, but its a lot faster and do the same work