UPFRONT DISCLAIMER: None of the code listed below has actually been tested. I was typing the code as I went along, without using any IDE for syntax corrections. The principles, however, have been applied time and again.
XML should be the easiest option for this application. As mentioned before, you can de-serialize the the XML to be represented as an array of Option objects, with associated display, value and type properties. A sample of the XML could be something like:
Code:
<Options>
<Option ID="1" Type="RadioButtons" Text="What is your gender?" Value="M">
<OptionItems>
<OptionItem ID="MaleItem" Text="Male" Value="M" />
<OptionItem ID="FemaleItem" Text="Female" Value="F" />
</OptionItems>
</Option>
<Option ID="2" Type="String" Text="Some Text" Value="Some Value" />
<Option ID="3" Type="DateTime" Text="Today's Date" Value="2009-03-23" />
</Options>
You could have the following class structure to implement XML serialization for the above-mentiond XML structure:
Code:
public class Options
{
private Option[] mOptions;
public Options() { }
[XmlArray]
public Option[] Options { get { return mOptions; } set { mOptions = value } }
}
public class Option
{
private int mID;
private string mText;
private object mValue;
private OptionType mType;
private OptionItem[] mOptionItems;
public Option() { }
[XmlAttribute]
public int ID { get { return mID; } set { mID = value; } }
[XmlAttribute]
public string Text { get { return mText; } set { mText = value; } }
[XmlAttribute]
public object Value { get { return mValue; } set { mValue = value; } }
[XmlAttribute]
public OptionType Type { get { return mType; } set { mType = value; } }
[XmlArray]
public OptionItem[] OptionItems { get { return mOptionItems; } set { mOptionItems = value; } }
}
public class OptionItem
{
private int mID;
private string mText;
private object mValue;
private OptionType mType;
private OptionItem[] mOptionItems;
public OptionItem() { }
[XmlAttribute]
public int ID { get { return mID; } set { mID = value; } }
[XmlAttribute]
public string Text { get { return mText; } set { mText = value; } }
[XmlAttribute]
public object Value { get { return mValue; } set { mValue = value; } }
}
public enum OptionType
{
[XmlEnum]
String = 1,
[XmlEnum]
DateTime = 2,
[XmlEnum]
Integer = 3,
[XmlEnum]
RadioButtons = 4
}
When you have the XML file and classes done, you can implement simple serialize / de-serialize methods to load the data from the XML file, have an in-memory object representation of it and finally write it back to disk:
Code:
public static class SystemOptions
{
private Options mOptions;
public static Options Load()
{
XmlSerializer serializer = new XmlSerializer(typeof(Options));
try
{
string FileName = "PATH TO OPTIONS XML FILE";
FileStream fs = new FileStream(FileName, FileMode.Open, FileAccess.Read);
mOptions = (Options)serializer.Deserialize(fs);
fs.Close();
}
catch (System.IO.FileNotFoundException)
{
mOptions = new Options();
}
return mOptions;
}
public static void Save()
{
string FileName = "PATH TO OPTIONS XML FILE";
XmlDocument doc = new XmlDocument();
StringBuilder xmlString = new StringBuilder();
XmlWriterSettings xmlSettings = new XmlWriterSettings();
xmlSettings.Indent = true;
XmlWriter xmlWriter = XmlWriter.Create(xmlString, xmlSettings);
try
{
XmlSerializer serializer = new XmlSerializer(typeof(Options));
serializer.Serialize(xmlWriter, mOptions);
doc.LoadXml(xmlString.ToString());
doc.Save(FileName);
}
catch
{
// Handle all your exceptions in here.
}
}
}
One last thing, for the code above to work, you'll have to import (at least) the following namespaces:
Code:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
By just calling the SystemOptions.LoadSystemOptions(); static method in your application, you can retrieve an array of Options objects to be used and manipulated to your heart's content. If you're done fiddling with the options

p) you can save it all back to disk by calling the SystemOptions.Save(); static method.
Right, I'm off. Hope you found this insightful!