Click here to Skip to main content
15,881,089 members
Articles / Web Development / ASP.NET

Using XML Serialization to Read Custom Configuration Elements

Rate me:
Please Sign up or sign in to vote.
2.75/5 (8 votes)
5 May 20044 min read 81.1K   520   32  
Custom configuration is a powerful tool provided by the .NET Framework. Ideas such as the Provider Model make heavy use of the configuration techniques. This article will demonstrate using XML Serialization to place objects into your configuration that can be used at run time.
using System;
using System.Xml.Serialization;

namespace XmlSerializationConfig
{
	/// <summary>
	/// Example class we'll place into the 
	/// configuration file for reading.
	/// </summary>
	[XmlRoot("Person")]
	public class Person
	{
		string __firstName = String.Empty;
		string __lastName = String.Empty;

		public Person()
		{
		}

		[XmlAttribute(DataType = "string", AttributeName = "Firstname")]
		public string Firstname
		{
			get { return __firstName; }
			set { __firstName = value; }
		}

		[XmlAttribute(DataType = "string", AttributeName = "Lastname")]
		public string Lastname
		{
			get { return __lastName; }
			set { __lastName = value; }
		}
	}
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
hmm.. stream of consciousness okay? i write, i teach, i code, i consult, live in charlotte, nc, have a blog at www.tatochip.com, have a fiance' named gina (who's a mac person!), listen to and spin drum'n'bass, deep house, and downtempo, and contemplate the illogical.

Comments and Discussions