Archive for December, 2008

In my previous serialization post, I wrote a class to serialize and deserialize to and from xml.
Well that class suffers from a couple of issues, to start it’s not thread safe, and also doesn’t make use of generics.
So I revisited the code, and introduced what I hope to be an improvement from previous version.

Here it is.

The Serialize method returns an xml string from a given object.

        /// <summary>
        /// Serializes the specified object to an xml string using the specified encoding.
        /// </summary>
        /// <typeparam name="T">The generic object type.</typeparam>
        /// <param name="source">The source.</param>
        /// <param name="encoding">The encoding to encode the returned xml string.</param>
        /// <returns>An Xml String with the object content.</returns>

        public static string Serialize<T>(T source, Encoding encoding)
        {
            // The string to hold the object content
            String content;

            // Create a memoryStream into which the data can be written and readed
            using (MemoryStream stream = new MemoryStream())
            {
                // Create the xml serializer, the serializer needs to know the type 
                // of the object that will be serialized
                XmlSerializer xmlSerializer = new XmlSerializer(source.GetType());

                // Create a XmlTextWriter to write the xml object source, we are going 
                // to define the encoding in the constructor
                using (XmlTextWriter writer = new XmlTextWriter(stream, encoding))
                {
                    // Save the state of the object into the stream
                    xmlSerializer.Serialize(writer, source);
                    // Flush the stream
                    writer.Flush();
                    // Read the stream into a string
                    using (StreamReader reader = new StreamReader(stream, encoding))
                    {
                        // Set the stream position to the begin
                        stream.Seek(0, SeekOrigin.Begin);
                        // Read the stream into a string
                        content = reader.ReadToEnd();
                    }
                }
            }

            // Return the xml string with the object content
            return content;
        }

 

The Deserialize method does the oposite, returns an object from an xml string.

        /// <summary>
        /// Deserializes the specified xml string to an object of type T using the specified encoding.
        /// </summary>
        /// <typeparam name="T">The new object.</typeparam>
        /// <param name="content">The xml string content.</param>
        /// <param name="encoding">The string encoding.</param>
        /// <returns>A new object of type T.</returns>

        public static T Deserialize<T>(string content, Encoding encoding)
        {
            // The object to be returned
            T source;

            // Create a memorystream into which the object string will be written
            using (MemoryStream stream = new MemoryStream())
            {
                // Create the sream writer with the specified encoding
                using (StreamWriter writer = new StreamWriter(stream, encoding))
                {
                    // Write the object content into the stream
                    writer.Write(content);

                    // Flush the stream
                    writer.Flush();

                    // Create the serializer, we must provide the object type to the constructor
                    Type type = typeof(T);
                    XmlSerializer xmlSerializer = new XmlSerializer(type);

                    // Set the stream position to the begin
                    stream.Seek(0, SeekOrigin.Begin);
                    // Deserialize the object
                    source = (T)xmlSerializer.Deserialize(stream);
                }
            }

            // Return the new object
            return source;
        }

 

Each method accept the string encoding as parameter, there is one overload for each method that uses UTF8 as the default encoding.

You can download the class from here.