您可以使用抱你贏得了數據的類導出(並導入)到XML。
您可以使用[Serializable]
使該類可用於xml導出。因此,舉例來說,如果你有這個類:
[Serializable]
public class MyClassThatKeepTheData
{
public List<int> cListWithValues;
public int Value1;
public int Value2;
}
然後你可以使用XmlSerializer將其轉換爲XML(和反之亦然)爲:
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;
}
,並轉換爲XML:
MyClassThatKeepTheData cTheObject = new MyClassThatKeepTheData();
ObjectToXML(typeof(MyClassThatKeepTheData), cTheObject)
相對: How to optimize class for viewstate
我建議也日e protobuf-net不是XML,但它快很多,並且做同樣的工作