Brendan Tompkins

Sponsors

The Lounge

Wicked Cool Jobs

News

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
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


Posted Tue, Mar 1 2005 11:03 AM by Brendan Tompkins

[Advertisement]

Comments

Scott Galloway wrote re: An XML Serialization Helper Class
on Tue, Mar 1 2005 9:05 AM
Possible (probable) I'm missing something - but you're using UTF8 to serialize and ASCII to deserialize - is that what you intended?
Brendan Tompkins wrote re: An XML Serialization Helper Class
on Tue, Mar 1 2005 9:22 AM
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.
Pedro Silva wrote re: An XML Serialization Helper Class
on Tue, Mar 1 2005 1:00 PM
I have a very similar helper class that does nearly the same thing, except the UTF-8 piece. Great minds, i guess... :)
TrackBack wrote A Strongly-Typed Cookie State Object
on Tue, Mar 15 2005 8:09 AM
dan wrote re: An XML Serialization Helper Class
on Mon, Dec 4 2006 2:20 PM

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

Michael Freidgeim wrote re: An XML Serialization Helper Class
on Mon, Jan 8 2007 6:04 PM

Thank you for the class.

To simplify copy & paste you should add

using System.Xml.Serialization;

buzzlight2nd@yahoo.com wrote re: An XML Serialization Helper Class
on Tue, Oct 14 2008 12:37 PM

Thank you for posting the great article. Unlike serialization to a file, very little is available for serialization to a memory stream. You saved me a lot of headaches!

Michael Freidgeim wrote re: An XML Serialization Helper Class
on Sat, Mar 21 2009 7:04 PM

Overload XmlDeSerialize(string xmlString, object serializableObject) is misleading, because the serializableObject parameter only determines the type,

it doesn't return back new object.

I've removed the overload from my local copy of the class.

Michael Freidgeim wrote re: An XML Serialization Helper Class
on Wed, Jul 1 2009 11:06 PM

I've also found useful to specify inside XmlSerializewriter.Formatting = Formatting.Indented;

Add a Comment

(required)  
(optional)
(required)  
Remember Me?
Devlicio.us