XML to Windows.Forms.Keys List It took me a lot of poking around and unit testing to get the code to look like it is right now. So I have a XML file which in part looks like this

&lt;FunctionKeys&gt;
    &lt;Name&gt;F13&lt;/Name&gt;
    &lt;Name&gt;F14&lt;/Name&gt;
    &lt;Name&gt;F15&lt;/Name&gt;
    &lt;Name&gt;F16&lt;/Name&gt;
&lt;/FunctionKeys&gt;


and So I want to take that data and put it back into my class.

This is in part what I have

    public override void Load(string elementText)
    {
        var ele = XElement.Parse(elementText);
        if (ele.Element("FunctionKeys").HasElements)
        {
            var funcs = ele.Element("FunctionKeys")
                .Descendants("Name")
                .Select(x=&gt;x.Value)
                .ToList();

            foreach (string s in funcs)
            {
                dliUnit.FunctionKeyList.Add(
                    (System.Windows.Forms.Keys)System.Enum.Parse(typeof(System.Windows.Forms.Keys),
                    s));
            }
        }
    }


Class to save to

public class DLIUnit
{
    public List&lt;Keys&gt; FunctionKeyList
    {
        get;set;
    }
    //Other Members
}


Somethign about the way that I parse the string back to the Enum (well the entire process really) doesn't sit well with me. I'm very bad at LINQ but have been trying hard to learn it and use it more and more when I play with XML. Is there a better/cleaner way to parse the FunctionKeys?

EDIT

I forgot to mention that I can change any portion of the code or XML file. Right now this is a new idea and can be changed to make it better.
Sticking with the approach you used how about this as an alternative.

public override void Load(string elementText)
    {
        var ele = XElement.Parse(elementText);

        var xElement = ele.Element("FunctionKeys");
        if (xElement != null &amp;&amp; xElement.HasElements)
        {
            _dliUnit.FunctionKeyList.AddRange(xElement
                               .Descendants("Name")
                               .Select(x =&gt; EnumHelper.GetEnum&lt;Keys&gt;(x.Value))
                );
        }
    }


I created a little Enum Extensions class just because I like typing GetEnum:

public static class EnumHelper
{
    public static T GetEnum&lt;T&gt;(string name)
    {
        if (IsValidEnumFor&lt;T&gt;(name))
            return (T)Enum.Parse(typeof(T), name);
        else
            throw new ArgumentException(typeof(T) + "does not contain a value member = " + name);
    }

    public static T GetEnum&lt;T&gt;(int number)
    {
        if (Enum.IsDefined(typeof(T), number))
        {
            return (T)Enum.ToObject(typeof(T), number);
        }
        else
        {
            throw new ArgumentException(typeof(T) + "does not contain a value member = " + number.ToString());
        }
    }

    public static bool IsValidEnumFor&lt;T&gt;(string name)
    {
        return Enum.IsDefined(typeof(T), name);
    }
}


Alternative - XmlSerializer

One alternative I have used in the past to parse Xml into objects is the .NET XmlSerializer.  In your case you might do something like:

StringReader sr = new StringReader(xml);

// Create an XmlSerializer object to perform the deserialization
XmlSerializer xs = new XmlSerializer(typeof(DLIUnit));
return (DLIUnit)xs.Deserialize(sr);


This may not work for you but I would recommend having a look into this class as it is fairly easy to use once you get going.  Once I got my head around the basics it helped immensely.