Click here to Skip to main content
15,880,392 members
Articles / Programming Languages / XML

Load and save objects to XML using serialization

Rate me:
Please Sign up or sign in to vote.
4.86/5 (126 votes)
1 Sep 2006CPOL7 min read 929.5K   23.1K   503  
A C# program to demonstrate loading and saving an object to an XML file using XML serialization encapsulated in a wrapper class.
using System;
using System.Xml.Serialization;	 //For serialization of an object to an XML Document file.

namespace disc1
{
	/// <summary>
	/// Type of destinations an e-mail address relates to.
	/// </summary>
	public enum EmailDestination
	{
		Home     = 0,
		Business = 1,
		Other    = 2
	}

	/// <summary>
	/// Custom class used to store an E-mail address.
	/// </summary>
	
	// Mark class as serializable.
	[Serializable]
	public class EmailAddress
	{
		/// <summary>
		/// Default constructor for this class (required for serialization).
		/// </summary>
		public EmailAddress()
		{
		}

		// Specify that this field should be serialized as an XML attribute 
		// instead of an element to demonstrate the formatting differences in an XML file. 
		[XmlAttribute]
		public string Address = null;

		// Specify that this field should be serialized as an XML attribute 
		// instead of an element to demonstrate the formatting differences in an XML file. 
		[XmlAttribute]
		public EmailDestination Destination = EmailDestination.Home;
	}
}

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
T-C
Web Developer
New Zealand New Zealand
TC is a software developer with a Degree in Information Systems and commercial experience ranging from C DLL's to N-Tier Web applications.

Comments and Discussions