[C#] Class serialiseren en deserialiseren
Enkel even een code dump deze keer:
public void Serialize(Configuration c)
{
XmlSerializer serializer = new XmlSerializer( c.GetType() );
StreamWriter streamwrite = new StreamWriter("config.xml");
serializer.Serialize(streamwrite, this);
streamwrite.Close();
}
public static Configuration Deserialize()
{
FileInfo file = new FileInfo("config.xml");
if (file.Exists)
{
StreamReader reader = new StreamReader("config.xml");
XmlSerializer ser = new XmlSerializer(typeof(Configuration));
Configuration c = (Configuration)ser.Deserialize(reader);
reader.Close();
return c;
}
else
return null;
}
Reacties
Een reactie posten