XML Serialization Code Sample

Problem
One is writing code an one needs to serialize an object.

Solution
One can use XML-based serialization, (as long as the object is xml-serializable), as shown below.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Schema;
using System.Xml.Serialization;
using System.Xml.XPath;
using System.Xml.Xsl;

namespace Test.Framework.Common.Core
{
	/// <summary>
	/// This is a basic serialization helper.
	/// </summary>
	/// <remarks>
	/// Reference...
	/// http://www.dotnetfunda.com/articles/show/articleShow.aspx?aid=98
	/// </remarks>
	public class SerializationHelper
	{
		#region ConstructorMethods

		public SerializationHelper()
		{

		}

		#endregion //ConstructorMethods

		#region HelperMethods

		/// <summary>
		/// This will deserialize any object that can use xml-based serialization.
		/// </summary>
		/// <param name="targetObject">This is the XML to deserialize.</param>
		/// <param name="targetType">This is type to use for the output.</param>
		/// <returns>This is the object deserialized.</returns>
		/// <remarks>
		/// Note the following RTE message that happens when trying to serialize a DataView...
		/// "To be XML serializable, types which inherit from ICollection must have an implementation of 
		/// Add(System.Data.DataRowView) at all levels of their inheritance hierarchy. 
		/// System.Data.DataView does not implement Add(System.Data.DataRowView)".
		/// </remarks>
		public object DeSerializeAnObject(string xmlOfAnObject, System.Type targetType)
		{
			object myObject = null;

			System.Xml.XmlReader myXmlReader = null;
			System.IO.StringReader myStringReader = null;

			try
			{
				myStringReader = new StringReader(xmlOfAnObject);

				System.Xml.Serialization.XmlSerializer myXmlSerializer =
					new System.Xml.Serialization.XmlSerializer(targetType);

				myXmlReader = new XmlTextReader(myStringReader);
				myObject = new object();
				myObject = (object)myXmlSerializer.Deserialize(myXmlReader);
			}
			catch (Exception ex)
			{
				throw new System.ApplicationException(
					"Exception caught and thrown. ex.ToString()='" + ex.ToString() + "'.");
			}
			finally
			{
				if (myXmlReader != null)
				{
					myXmlReader.Close();
				}

				if (myStringReader != null)
				{
					myStringReader.Close();
					myStringReader.Dispose();
				}
			}

			return myObject;
		}

		/// <summary>
		/// This will serialize any object that can use xml-based serialization.
		/// </summary>
		/// <param name="targetObject">This is the object to serialize.</param>
		/// <returns>This is the object serialized as XML.</returns>
		/// <remarks>
		/// Note the following RTE message that happens when trying to serialize a DataView...
		/// "To be XML serializable, types which inherit from ICollection must have an implementation of 
		/// Add(System.Data.DataRowView) at all levels of their inheritance hierarchy. 
		/// System.Data.DataView does not implement Add(System.Data.DataRowView)".
		/// </remarks>
		public string SerializeObject(object targetObject)
		{
			string myXml = "";

			if (targetObject == null)
			{
				throw new System.ApplicationException("The given object, targetObject, is null.");
			}

			System.IO.MemoryStream myMemoryStream = null;

			try
			{
				myMemoryStream = new System.IO.MemoryStream();

				System.Xml.Serialization.XmlSerializer mySerializer =
					new System.Xml.Serialization.XmlSerializer(targetObject.GetType());

				mySerializer.Serialize(myMemoryStream, targetObject);
				myMemoryStream.Position = 0;
				System.Xml.XmlDocument myXmlDoc = new XmlDocument();
				myXmlDoc.Load(myMemoryStream);
				myXml = myXmlDoc.InnerXml;
			}
			catch (Exception ex)
			{
				throw new System.ApplicationException(
					"Exception caught and thrown. ex.ToString()='" + ex.ToString() + "'.");
			}
			finally
			{
				if (myMemoryStream != null)
				{
					myMemoryStream.Close();
					myMemoryStream.Dispose();
				}
			}

			return myXml;
		}

		#endregion //HelperMethods
	}
}

HTH.

Thank you.

— Mark Kamoski

Advertisements

Author: mkamoski1

n/a