Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Helper for Serializing to a String

0.00/5 (No votes)
11 Jul 2013 1  
Helper for serializing objects to a string

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://www.w3.org/2001/XMLSchema-instance\">http://www.w3.org/2001/XMLSchema-instance\</a>"" ;
        private const string xmlnsString2 = "xmlns:xsd=\"<a href="http://www.w3.org/2001/XMLSchema\">http://www.w3.org/2001/XMLSchema\</a>"";

        /// <summary>
        /// Serialize an object to a string.
        /// </summary>
        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;
            }
        }
    }

    /// <summary>
    /// Deserialize an string into an object
    /// </summary>
    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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here