[C#] SOAP Format Serialization/Deserialization - Class Object

AceInfinity

Emeritus, Contributor
Joined
Feb 21, 2012
Posts
1,728
Location
Canada
Here's an example of serialization I've put together. What this allows you to do is to retrieve data from a file and save class object data to that file for later use. You'll notice the serializable object we have, is our TLFUser class marked with the Serializable attribute, so that we can serialize this object using the SOAP format.

Before you begin, you'll need to add these to the top of your code for references:
C#:
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Soap;

And you'll also need to manually add the reference for System.Runtime.Serialization.Formatters.Soap.

Here's the data we want to serialize:
C#:
[Serializable]
public class TLFUser
{
	public string Username;
	public int UID;
	public string GroupName;
	public int GID;

	public TLFUser(string name, int uid, string groupname, int gid)
	{
		this.Username = name;
		this.UID = uid;
		this.GroupName = groupname;
		this.GID = gid;
	}

	public string DisplayData()
	{
		return string.Format("{0}\r\n{1}\r\n{2}\r\n{3}", this.Username, this.UID, this.GroupName, this.GID);
	}
}

And here's our method:
C#:
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;
}

You should be able to see how this can come in useful, otherwise ask your questions :)

:beerchug2:
 
Probably improved to check if the type of the type parameter T is serializable or not, by checking if that Serializable property is there or not.

C#:
if (!typeof(T).IsSerializable) return false;

Which leads me to my SOAP Serializer class that i've created:
C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Soap;

namespace SOAPFormat
{
	public static class SOAPdata
	{
		public static bool SOAPSerialize<T>(this T ClassData, string filePath)
		{
			if (!typeof(T).IsSerializable) return false;
			FileStream fs = new FileStream(filePath, System.IO.FileMode.Create, FileAccess.Write);
			try
			{
				SoapFormatter soap = new SoapFormatter();
				soap.Serialize(fs, ClassData);
			}
			catch (SerializationException)
			{
				return false;
			}
			finally
			{
				fs.Close();
			}

			return true;
		}

		public static bool SOAPDeserialize<T>(this string filePath, out T ClassObj)
		{
			ClassObj = default(T);
			if (!typeof(T).IsSerializable || !File.Exists(filePath)) return false;
			FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
			try
			{
				SoapFormatter soap = new SoapFormatter();
				ClassObj = (T)soap.Deserialize(fs);
			}
			catch (SerializationException)
			{
				return false;
			}
			finally
			{
				fs.Close();
			}

			return true;
		}
	}
}

And I combined it with an AppConfig class which will assign values to an App Information class of your choice, and from which you can load as settings for your entire application if you wanted:

C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;
using SOAPFormat;

namespace AppSettings
{
	public static class AppConfig
	{
		public static bool LoadConfig<T>(string configFile, out T appConfig)
		{
			appConfig = default(T);
			if (File.Exists(configFile))
			{
				return configFile.SOAPDeserialize<T>(out appConfig);
			}
			return false;
		}

		public static bool SaveConfig<T>(string configFile, T appInfo)
		{
				return appInfo.SOAPSerialize<T>(configFile);
		}
	}
}

I actually think this is quite useful if you want a configurable application. Now, note that this app config class is pretty useless, unless you're going to put the actual loading of each of the settings from that returned class instance to a property in your app? This was just a dummy setup that I was testing, so you'll have to turn them from bool's into void's, and assign what you want here. I'm leaving that open for you to do :)


It would be much more useful if it avoided return values of bool, and just directly applied the configuration to where it was intended to be set.

:beerchug2:
 
Last edited:

Has Sysnative Forums helped you? Please consider donating to help us support the site!

Back
Top