Introduction
On a recent project I was working on, there was a requirement to save and load the state of various objects used by a database application. The solution was to add serialization functionality to the classes containing the application data.
The initial solution was to build a Visual Studio item template that could be used to quickly create classes that contained built-in capabilities of reading and writing the class property values via XML formatted files or string
s.
A second solution was to build a generic class that takes any class type as a parameter and provides the same read/write XML functionality.
Both solutions provide serialization to files, string
s and database tables. Methods that encrypt the serialized values are included for each type of serialization.
Class Template for Visual Studio
The .NET XML serialization namespace was used to provide the XML serialize/de-serialize functionality.
The System.Reflection
namespace was used to provide methods that output the contents of the objects to non-XML formatted string
values.
The attached source code does not include any binary or SOAP serialization functionality.
See the following link for the steps needed to add a custom project item template to the Add New Item dialog box:
public class ClassTemplateWithSerializers
{
.....
public void SaveToXmlFile(string filePath)
{
XmlSerializer ser = new XmlSerializer(typeof(ClassTemplateWithSerializers));
TextWriter tex = new StreamWriter(filePath);
ser.Serialize(tex, this);
tex.Close();
}
public static ClassTemplateWithSerializers LoadFromXmlFile(string filePath)
{
XmlSerializer deserializer = new XmlSerializer(typeof(ClassTemplateWithSerializers));
TextReader textReader = new StreamReader(filePath);
ClassTemplateWithSerializers columnDefinitions;
columnDefinitions = (ClassTemplateWithSerializers)deserializer.Deserialize(textReader);
textReader.Close();
return columnDefinitions;
}
Class Manager using Generics
If you do not wish to repeat the serialization logic in each class, you can use the ClassManager
module. This module uses generics to accept an instance of a class type as a parameter to the constructor. Its methods then operate generically on the class instance.
public class ClassManager<T>
{
.....
public void SaveToXmlFile(string filePath)
{
XmlSerializer ser = new XmlSerializer(typeof(T));
TextWriter tex = new StreamWriter(filePath);
ser.Serialize(tex, _classInstance);
tex.Close();
}
public static T LoadFromXmlFile(string filePath)
{
XmlSerializer deserializer = new XmlSerializer(typeof(T));
TextReader textReader = new StreamReader(filePath);
T columnDefinitions;
columnDefinitions = (T)deserializer.Deserialize(textReader);
textReader.Close();
return columnDefinitions;
}
Using the Code
private static ClassWithSerializers _classWithSerializers = new ClassWithSerializers();
…..
classWithSerializers.IntValue = 25321;
…...
_classWithSerializers.SaveToXmlFile(outputPath);
…..
ClassWithSerializers newObject = new ClassWithSerializers();
newObject = ClassWithSerializers.LoadFromXmlFile(outputPath);
private static ClassManager<TestClassNoSerializers> _classManager;
.....
_classManager.SaveToXmlFile(outputPath);
....
_classManager.SaveToXmlFile(outputPath);
newObject2 = ClassManager<TestClassNoSerializers>.LoadFromXmlFile(outputPath);
ClassManager<TestClassNoSerializers> clsmgr2 = new ClassManager<TestClassNoSerializers>(newObject2);
outputString = clsmgr2.ToXmlString();
.....
Points of Interest
Only XML serialization is used for this code. This approach was taken to help with troubleshooting any application issues.
Where security is required, serialization methods that use AES encryption were included in both the class template and generic class manager.
Saving to a database was coded by building methods that use the System.Data.Odbc
functionality provided by .NET. In a real-world scenario, you could modify this code to use another .NET database provider.
History
This is the initial public release of the code.