private void MainMethod()
{
SerializeData(SerializeOptions.Deserialize);
}
private enum SerializeOptions
{
Serialize,
Deserialize
}
private void SerializeData(SerializeOptions Opt)
{
string filePath = string.Format(@"{0}\test.xml", Environment.GetFolderPath(Environment.SpecialFolder.Desktop));
TLFUser TLFuser;
switch (Opt)
{
case SerializeOptions.Serialize:
TLFuser = new TLFUser("AceInfinity", 1, "Administrators", 4);
if (SOAPSerialize<TLFUser>(filePath, TLFuser))
{
MessageBox.Show("Successful serialization!");
}
break;
case SerializeOptions.Deserialize:
if (SOAPDeserialize<TLFUser>(filePath, out TLFuser))
{
MessageBox.Show("Successful deserialization!");
textBox1.Text = TLFuser.DisplayData();
}
break;
}
}
private bool SOAPSerialize<T>(string filePath, T ClassData)
{
FileStream fs = new FileStream(filePath, System.IO.FileMode.CreateNew);
try
{
SoapFormatter soap = new SoapFormatter();
soap.Serialize(fs, ClassData);
}
catch (SerializationException)
{
return false;
}
finally
{
fs.Close();
}
return true;
}
private bool SOAPDeserialize<T>(string filePath, out T ClassObj)
{
FileStream fs = new FileStream(filePath, FileMode.Open);
try
{
SoapFormatter soap = new SoapFormatter();
ClassObj = (T)soap.Deserialize(fs);
}
catch (SerializationException)
{
ClassObj = default(T);
return false;
}
finally
{
fs.Close();
}
return true;
}