Thursday, February 04, 2010

How to create config file with xml serialization

This is a simple example of how to use xml serialization to make configuration files.

In many cases there is no need to use System.Configuration namespace members for application configuration file. This article will focus on XML serialization.

The main idea is to create class which holds configuration data and then, serialize it to a file using XmlSerializer. Classes that can be serialized using XML must be declared as public and have a parameterless constructor. Also serialized members must be public.

For more information about XML serialization please refer to http://msdn.microsoft.com/en-us/library/182eeyhh.aspx

To serialize and deserialize data use corresponding methods from XmlSerializer class (System.Xml.Serialization). Both of them operate on stream objects.

The following code sample demonstrates creating, reading and saving of simple configuration file.

Read more: C# Corner

Posted via email from jasper22's posterous