Click here to Skip to main content
15,891,761 members
Articles / Programming Languages / XML

Wrap the .NET Xml Serialization Framework

Rate me:
Please Sign up or sign in to vote.
2.64/5 (11 votes)
1 Jun 20032 min read 41.7K   19  
This lightweight base class trivializes Xml Serialization and eliminates duplication in projects with multiple serializable classes.
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
/// <summary>
///   This class serves as a base class for classes wishing to
///   leverage Xml Serialization. The Serialization mechanism is 
///   abstracted by the object/instance method Save(). The Deserialization 
///   mechanism is abstracted by the class/static method Load()
/// </summary>
namespace Romney.Christian.Xml.Serialization
{
	public abstract class XmlSerializationBase
	{
		public void Save(string sFilePath)
		{			
			Save(File.OpenWrite(sFilePath), true);			
		}		
		public void Save(FileStream fs)
		{			
			Save(fs, true);
		}
		public void Save(FileStream fs, bool AutoClose)
		{			
			XmlSerializer ser = null;			
			try
			{
				ser = new XmlSerializer(this.GetType());
				ser.Serialize(fs, this);			
			}
			finally
			{
				if(AutoClose)
				{
					fs.Close();
				}
			}
		}

		public void Save(XmlWriter writer)
		{
			Save(writer, true);
		}
		public void Save(XmlWriter writer, bool AutoClose)
		{
			XmlSerializer ser = null;
			try
			{
				ser = new XmlSerializer(this.GetType());
				ser.Serialize(writer, this);			
			}
			finally
			{
				if(AutoClose)
				{
					writer.Close();
				}
			}
		}
		public void Save(TextWriter writer)
		{
			Save(writer, true);
		}
		public void Save(TextWriter writer, bool AutoClose)
		{
			XmlSerializer ser = null;
			try
			{
				ser = new XmlSerializer(this.GetType());
				ser.Serialize(writer, this);			
			}			
			finally
			{
				if(AutoClose)
				{
					writer.Close();
				}
			}
		}

		public static XmlSerializationBase Load(string sFilePath, Type objectType)
		{
			return Load(File.OpenRead(sFilePath), objectType, true);			
		}
		public static XmlSerializationBase Load(FileStream fs, Type objectType)
		{
			return Load(fs, objectType, true);
		}
		public static XmlSerializationBase Load(FileStream fs, Type objectType, bool AutoClose)
		{
			XmlSerializer ser = null;			
			try
			{
				ser = new XmlSerializer(objectType);				
				return  (XmlSerializationBase)ser.Deserialize(fs);			
			}
			finally
			{
				if(AutoClose)
				{
					fs.Close();
				}
			}
		}
		public static XmlSerializationBase Load(XmlReader reader, Type objectType)
		{
			return Load(reader, objectType, true);
		}
		public static XmlSerializationBase Load(XmlReader reader, Type objectType, bool AutoClose)
		{
			XmlSerializer ser = null;			
			try
			{
				ser = new XmlSerializer(objectType);				
				return  (XmlSerializationBase)ser.Deserialize(reader);			
			}
			finally
			{
				if(AutoClose)
				{
					reader.Close();
				}
			}
		}
		public static XmlSerializationBase Load(TextReader reader, Type objectType)
		{
			return Load(reader, objectType, true);
		}
		public static XmlSerializationBase Load(TextReader reader, Type objectType, bool AutoClose)
		{
			XmlSerializer ser = null;			
			try
			{
				ser = new XmlSerializer(objectType);				
				return  (XmlSerializationBase)ser.Deserialize(reader);			
			}
			finally
			{
				if(AutoClose)
				{
					reader.Close();
				}
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


Written By
Web Developer
United States United States
I am the Senior Manager, Ecommerce Development for Certified Vacations, which develops and maintains websites for some of the biggest names in the travel industry.

I've been programming Microsoft platforms for over 7 years. My interests include angle brackets and my family.

Comments and Discussions