Introduction
I had a need to save a copy of a local variable to a class
, but the Settings
does not seem to support objects that are of a Type
that is defined in the local project, so I could have created a special serializer, but decided that I wanted to create something that was more generic, and since wanted to save it in the Settings
file, converting it to a string
seemed like the best solution, and using XML would be a preferred format since it is easy to understand what is being saved. The next problem was that the XmlSerializer
needs a Stream
for conversion. With the complexity associate with handling the Stream
, it seemed best to encapsulate the functionality using generics. Since there is no state required, it is preferable to implement these methods as static
.
Potential Serializer Gotchas
There are a number of issues that you can run into when serializing, and the compiler does not seem to be much help and the error messages are not much either:
- Classes serialized must be
public
- All properties that are marked to be serialized must have both a setter and getter.
The Code
The class that does the serialization and deserialization is two short static
methods in a static
class
:
public static class Serializer
{
private const string xmlnsString1 = "xmlns:xsi=\"<a href="http:
private const string xmlnsString2 = "xmlns:xsd=\"<a href="http:
public static string SerializedToString<T>(T serializableObject)
{
if (serializableObject.Equals(default(T))) return string.Empty;
try
{
var serializer = new XmlSerializer(typeof(T));
using (var stringWriter = new StringWriter())
{
serializer.Serialize(stringWriter, serializableObject,
new XmlSerializerNamespaces());
var str = stringWriter.ToString().Replace(xmlnsString1,
string.Empty).Replace(xmlnsString2, string.Empty);
return str;
}
}
catch (Exception ex)
{
return string.Empty;
}
}
}
public static T DeSerializeFromString<t>(string serializedObject)
{
if (string.IsNullOrWhiteSpace(serializedObject)) return default(T);
try
{
var stringReader = new StringReader(string.Format(serializedObject,
headerString, xmlnsString));
var serializer = new XmlSerializer(typeof(T));
return (T)serializer.Deserialize(stringReader);
}
catch (Exception exception)
{
return default(T);
}
}
}
There is really no error handling, but it may be desirable to put it in. Also, the code removes some text that is extremely repetitive: the XML declaration line, and the declaration of the namespaces. The code is done so that if something besides what the code expects, there will be no harm, but some may think that the saved space is not worth it.
Summary
These methods makes it very easy to serialize an object to and from a string, but is not really appropriate for saving the serialization since it would the XMLSerializer
supports a type of Stream
, and can use a Stream to more directly support saving to file. There is also disadvantages of possible changes in the object that will not be supported unless there is some customization done on the objects for serialization, and XML is not an efficient way to save data.
Has been working as a C# developer on contract for the last several years, including 3 years at Microsoft. Previously worked with Visual Basic and Microsoft Access VBA, and have developed code for Word, Excel and Outlook. Started working with WPF in 2007 when part of the Microsoft WPF team. For the last eight years has been working primarily as a senior WPF/C# and Silverlight/C# developer. Currently working as WPF developer with BioNano Genomics in San Diego, CA redesigning their UI for their camera system. he can be reached at qck1@hotmail.com.