0

I have enum class that looks like this:

public enum StupidEnum
    {

        /// <remarks/>
        [System.Xml.Serialization.XmlEnumAttribute("01")]
        Item01,

        /// <remarks/>
        [System.Xml.Serialization.XmlEnumAttribute("01_1")]
        Item01_1,

        /// <remarks/>
        [System.Xml.Serialization.XmlEnumAttribute("01_11")]
        Item01_11,
}

And now I would like to get Item01_11 enum object by only giving 01_11 value. In other words I have value from Xml Enum Attribute and as a result I would need enum object to be returned.

2
  • Do you mean you wish to decode 01_11 from your code instead of from deserializing the enum? Commented Nov 6, 2017 at 8:18
  • I'm getting string value from database that is in this form "01_11" and I would want to parse it to enum value StupidEnum.Item01_11 Commented Nov 6, 2017 at 9:55

2 Answers 2

1

I have managed to get it working but it's not a very elegant solution. Since your attributes are dependent on XML serialization you'll need to read it that way. I'll post the working code, and the way I would recommend.

var testString = "01_1";
var xml = new XmlSerializer(typeof(StupidEnum));
// XmlSerializer expects XML so wrap what you got in xml tags.
using(var ms = new MemoryStream(Encoding.ASCII.GetBytes($"<StupidEnum>{testString}</StupidEnum>")))
    var item = (StupidEnum)xml.Deserialize(ms);
    Console.WriteLine(item);
}

Alternatively, you can cast the enum to an integral type and store that number then cast back to your enum when reading or use the enumerations own ability to parse a string and not rely on the xml serialization values as follows:

var testString = "Item01_1";  
// you can also just get the 01_1 and concatenate it to "Item" when parsing in the next line
var value = Enum.Parse(typeof(StupidEnum), testString);
Console.WriteLine(value);
Sign up to request clarification or add additional context in comments.

Comments

0

You can create a Utility class and method like below:

public static class EnumUtilities
{
    public static T GetValueFromXmlEnumAttribute<T>(string value) where T : Enum
    {
        foreach (var field in typeof(T).GetFields())
        {
            if (Attribute.GetCustomAttribute(field,
            typeof(XmlEnumAttribute)) is XmlEnumAttribute attribute)
            {
                if (attribute.Name == value)
                    return (T)field.GetValue(null);
            }
            else
            {
                if (field.Name == value)
                    return (T)field.GetValue(null);
            }
        }

        throw new ArgumentException("Could not parse XmlEnumAttribute. Not found.", nameof(value));
    }

And call it like:

var result = EnumUtilities.GetValueFromXmlEnumAttribute<StupidEnum>("01");

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.