Click here to Skip to main content
13,191,877 members (65,506 online)
Click here to Skip to main content
Add your own
alternative version


9 bookmarked
Posted 22 Aug 2007

Simple settings using Generics and XML serialization

, 22 Aug 2007
Rate this:
Please Sign up or sign in to vote.
An easy and very dynamic way of saving your settings.


Very often, when coding an application, you need to store your settings that you can easily load when the program starts. I guess most people use the app.config or settings, but I don't think using those is the best solution. Therefore, I came up with this very easy to use class that enables you to store any type of class to an XML file that can be reloaded very easily.

Using the code

The XmlSettings class uses XML serialization, a very basic and extremely fast method. The Load method takes XMLPath as an argument, and returns our type.

The Save method also takes the XMLpath argument and the T settings argument. T settings is the type you want to be serialized.

public static T Load(string XMLPath)
     //Load settings and return our type

     T temp;
     XmlSerializer DeSerializer = new XmlSerializer(typeof(T));
     TextReader reader = new StreamReader(XMLPath);
     temp = (T)DeSerializer.Deserialize(reader);
     return temp;

public static void Save(string XMLPath, T settings)
     //Save settings to xml using our type
     XmlSerializer serializer = new XmlSerializer(typeof(T));
     TextWriter writer = new StreamWriter(XMLPath);
     serializer.Serialize(writer, settings);

Using the XMLSettings class is very basic.

When you want to save settings, first create your settings class with the variables you need in your program. Make sure it has a parameterless constructer. Give it the path to your XML file, and make sure all folders are created. Then, pass your settings class as well.

XMLSettings<MySettings>.Save(XMLPath, Settings);

When loading, the Load method simply returns your type. Just pass the XML path to the method, and voila!

setting = XMLSettings<MySettings>.Load(XMLPath);


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


About the Author

Lasse Offt
Denmark Denmark
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionString Name Required? Pin
Dean K11-Sep-07 22:03
memberDean K11-Sep-07 22:03 
AnswerRe: String Name Required? Pin
Wb-FreeKill11-Sep-07 22:49
memberWb-FreeKill11-Sep-07 22:49 
AnswerRe: String Name Required? Pin
Dean K11-Sep-07 22:58
memberDean K11-Sep-07 22:58 
GeneralRe: String Name Required? Pin
Wb-FreeKill11-Sep-07 23:18
memberWb-FreeKill11-Sep-07 23:18 
GeneralRe: String Name Required? Pin
Dean K12-Sep-07 15:17
memberDean K12-Sep-07 15:17 
GeneralRe: String Name Required? Pin
Wb-FreeKill12-Sep-07 21:12
memberWb-FreeKill12-Sep-07 21:12 
GeneralRe: String Name Required? Pin
Dean K13-Sep-07 0:01
memberDean K13-Sep-07 0:01 
GeneralRe: String Name Required? Pin
Wb-FreeKill13-Sep-07 0:03
memberWb-FreeKill13-Sep-07 0:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171017.2 | Last Updated 23 Aug 2007
Article Copyright 2007 by Lasse Offt
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid