2
\$\begingroup\$

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

<FunctionKeys>
    <Name>F13</Name>
    <Name>F14</Name>
    <Name>F15</Name>
    <Name>F16</Name>
</FunctionKeys>

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=>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<Keys> 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.

\$\endgroup\$
4
  • \$\begingroup\$ Could you use XmlSerializer to do what you are after or would you prefer to stick with this approach? \$\endgroup\$
    – dreza
    Commented Apr 30, 2013 at 20:34
  • \$\begingroup\$ @dreza If it is easier. The other data types I have for DLIUnit are all basic: FileInfo, String, List<string>, List<Keys> are it. \$\endgroup\$ Commented Apr 30, 2013 at 20:37
  • \$\begingroup\$ @dreza woudl it help if i posted my unit test? \$\endgroup\$ Commented Apr 30, 2013 at 20:38
  • \$\begingroup\$ I don't think you need to post your Tests although nice that you have them. As a side note. Something that really helped my Linq abilities was Resharper. I would recommend that if this is something you are looking to improve in. \$\endgroup\$
    – dreza
    Commented Apr 30, 2013 at 20:46

1 Answer 1

2
\$\begingroup\$

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 && xElement.HasElements)
        {
            _dliUnit.FunctionKeyList.AddRange(xElement
                               .Descendants("Name")
                               .Select(x => EnumHelper.GetEnum<Keys>(x.Value))
                );
        }
    }

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

public static class EnumHelper
{
    public static T GetEnum<T>(string name)
    {
        if (IsValidEnumFor<T>(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<T>(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<T>(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.

\$\endgroup\$
1
  • \$\begingroup\$ Brilliant i'll give a go tomorrow \$\endgroup\$ Commented Apr 30, 2013 at 21:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.