CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Brendan Tompkins [MVP]

Blog First. Ask Questions Later.

An XML Serialization Helper Class

David’s gearing up a conversation about XML Serialization of application settings (see XML Serialization - Conversion of XML Documents and Streams to Common Language Runtime Objects and Vice Versa), and I thought I’d offer up a class that I use heavily in my day to day development. It’s a serialization helper, and it just contains some static methods for serializing objects back and forth from/to XML.


Updated: Changed to use Encoding.UTF8 as per Scott G.'s suggestion.

  public class SerializationHelper

  {

    /// <summary>

    /// Serialize an object into XML

    /// </summary>

    /// <param name="serializableObject">Object that can be serialized</param>

    /// <returns>Serial XML representation</returns>

    public static string XmlSerialize(object serializableObject)

    { 

      XmlSerializer serializer = new XmlSerializer(serializableObject.GetType());

      System.IO.MemoryStream aMemStr = new System.IO.MemoryStream();

      System.Xml.XmlTextWriter writer = new System.Xml.XmlTextWriter(aMemStr, null);

      serializer.Serialize(writer,serializableObject);

      string strXml = System.Text.Encoding.UTF8.GetString(aMemStr.ToArray());

      return strXml;     

    }

 

    /// <summary>

    /// Restore (Deserialize) an object, given an XML string

    /// </summary>

    /// <param name="xmlString">XML</param>

    /// <param name="serializableObject">Object to restore as</param>

    public static object XmlDeSerialize(string xmlString, object serializableObject)

    {

      XmlSerializer serializer = new XmlSerializer(serializableObject.GetType());     

      System.IO.MemoryStream aStream = new

        System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(xmlString));

 

      return serializer.Deserialize(aStream);

    }

 

    /// <summary>

    /// Restore (Deserialize) an object, given an XML string

    /// </summary>

    /// <param name="xmlString">XML</param>

    /// <param name="serializableObject">Type of object to restore as</param>

    public static object XmlDeSerialize(string xmlString, Type objectType)

    {

      XmlSerializer serializer = new XmlSerializer(objectType);     

      System.IO.MemoryStream aStream = new

        System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(xmlString));

 

      return serializer.Deserialize(aStream);

    }

  }

To Serialize an object:

[AirCode]
SomeObjectType someObject = new SomeObjectType();
String someString = SerializationHelper.XmlSerialize(someObject)

To DeSerialize an object

[AirCode]
someObject = (SomeObjectType) SerializationHelper.XmlDeSerialize(someString, typeof(SomeObjectType));

-Brendan



Comments

Scott Galloway said:

Possible (probable) I'm missing something - but you're using UTF8 to serialize and ASCII to deserialize - is that what you intended?
# March 1, 2005 9:05 AM

Brendan Tompkins said:

Scott, this is a good question, and I never noticed this (this is ancient code and I'm not sure where the original source was). My only problem with converting this to UTF8 in the above example, is that I know that UTF8 is more strict, shouldn't matter in this case, however, but I'm hesitant to change the code without some discussion.

Now, if you did the serialzation yourself using UTF8, it's a no-brainer.
# March 1, 2005 9:22 AM

Pedro Silva said:

I have a very similar helper class that does nearly the same thing, except the UTF-8 piece. Great minds, i guess... :)
# March 1, 2005 1:00 PM

TrackBack said:

# March 15, 2005 8:09 AM

dan said:

VB.Net code for this would be great. Thank you anyway :)

# December 4, 2006 2:20 PM

Michael Freidgeim said:

Thank you for the class.

To simplify copy & paste you should add

using System.Xml.Serialization;

# January 8, 2007 6:04 PM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add

About Brendan Tompkins

Brendan has been programming with .NET since the first public beta and is owner and operator of Port Technology Services, a consultancy company providing .NET application development services to the Maritime industry. In July, 2007, he was awarded the Microsoft MVP award for ASP.NET. He's also a proud co-founder of failed .COM startup Intrinsigo, and has had a hand in the failure of numerous other businesses. He currently runs CodeBetter.Com and Devlicio.us, and lives in Norfolk, Virgina with his wife Tiara and son Ian.

View Brendan's profile on LinkedIn

Check out Devlicio.us!