How do you retrieving the XmlEnumAttribute values for an Enum

When you are playing with Web Services you quite often find information held in an Attribute, but how do you get it out?

e.g.

public enum Classification {

        [System.Xml.Serialization.XmlEnumAttribute(“Test drive demonstrator”)]
        Testdrivedemonstrator,

        [System.Xml.Serialization.XmlEnumAttribute(“Showroom vehicle”)]
        Showroomvehicle,

 

By using reflection you can gain access to the attribute, here is how:

Type enumType = typeof(velocityUom);

foreach (FieldInfo fi in enumType.GetFields())

{

object[] attrs = fi.GetCustomAttributes(typeof(XmlEnumAttribute), false);

if (attrs.Length > 0)

{

Console.WriteLine(((XmlEnumAttribute)attrs[0]).Name);

}

}