tags:

views:

52

answers:

3

I am using C# with the .NET 3.5 framework to write an application.

I have an enum like this,

public static enum SettingOneType { FooA, FooB, FooC }

I also have an XDocument that I load like this in a Load() method,

LoadXML(){
    ...
    XDocument SettingsDocument;
    if(File.Exists(path)
    {
    XElement SettingsElement = new XElement("DeviceSettings",
         new XElement("Setitng1", SettingOneType.FooA.ToString()),
         new XElement("Setting2", ...  ));


    XDeclaration dec = new XDeclaration("1.0", "UTF-8", "yes");
    SettingsDocument = new XDocument(dec, SettingsElement);
    SettingsDocument.Save(xpath);
    }
    else SettingsDocument = XDocument.Load(path);
}

What I am wondering is, is there a way to read these settings in a strongly typed manner. Because I want to have a Property in my application that will access the value in the xml file like this...

public SettingOneType SettingOne
{
    get{
        SettingOneType x = SettingsDocument. //Here I know I can use LINQ statements to file the value I want but    is there a way to cast the value to the correct type without using a giant switch statement or something?
    }
}

NOTE: Before someone suggests that I use the built in Application Settings files that are available with .NET, don't bother. I usually do use those but for this project there is a reason that I can't that I don't want to explain.

A: 

Having the enumeration value as a string you could use Enum.Parse to parse it:

string foo = "FooA";
SettingOneType settingOneType =
    (SettingOneType)Enum.Parse(typeof(SettingOneType), foo);
Jeroen
+1  A: 

you can use some form of serialization (such as the DataContractSerializer) to persist a typed object to XML. You can then read the entire object back in a typesafe way instead of manually handling individual XML nodes.

For example, create a DocumentSettings class and persist it to XML using code like the following:

internal string Serialize(Object documentSettings)
{
   StringBuilder serialXML = new StringBuilder();
   DataContractSerializer dcSerializer = new DataContractSerializer(obj.GetType());
   using (XmlWriter xWriter = XmlWriter.Create(serialXML))
   {
       dcSerializer.WriteObject(xWriter, obj);
       xWriter.Flush();
       return serialXML.ToString();
   }
}

I'll leave it to you to figure out the deserialization code (its pretty simple)

Addys
So this would read and save all of the settings at once?
Jordan S
Yes. I also recommend this, it's heaps easier than the alternatives, unless you have huge quantities of data - then maybe an alternative should be considered. And I do mean huge - XML serialization is _quite_ performant.
Alex Paven
JordanS: Yes. Updating a single settings usually involves reading the entire class, changing the value and serializing everything back to file. The overhead is pretty minimal for small classes, but obviously if your settings files are huge then this isn't the best approach.
Addys
A: 

Store the XML values as numbers, then you just int.parse the string value, and cast it to the enum type. Each enum member corresponds to an int value by default and will cast appropriately, they increment from 0 up by default in the order they're defined.

When you create the values instead of:

new XElement("Setitng1", SettingOneType.FooA.ToString())

do:

new XElement("Setitng1", ((int)SettingOneType.FooA).ToString())

Then you read them back:

SettingOneType mySetting = (SettingOneType)int.Parse(myXElem.Value);

You might be able to just do an implicit cast there, I don't recall but that's the jist anyway..

Jimmy Hoffa