all groups > dotnet xml > may 2006 >
You're in the

dotnet xml

group:

Subject: retrieving the XmlEnumAttribute values for an Enum



Subject: retrieving the XmlEnumAttribute values for an Enum Edward Clements
5/14/2006 9:51:02 AM
dotnet xml: I have an enum defined as
public enum velocityUom
{ /// <remarks/>
[System.Xml.Serialization.XmlEnumAttribute("m/s")]
ms,

/// <remarks/>
[System.Xml.Serialization.XmlEnumAttribute("cm/a")]
cma,
...
}

This class was generated by xsd.exe, from a schema like
<xsd:simpleType name="velocityUom">
<xsd:enumeration value="m/s"/>
<xsd:enumeration value="cm/a"/>
....
</xsd:restriction>
</xsd:simpleType>

I would like to retrieve the XmlEnumAttribute values for this Enum so that I
can create a combo box with values like "m/s", "cm/a", ... so that I don't
have to hard-code the XML-enum values in my code.

Is there any way to do this? the MSDN examples are only to add extra enum
values for a class and Enum.GetNames() just retrieves "ms", "cma", ...

Thanks,

Edward Clements
RE: Subject: retrieving the XmlEnumAttribute values for an Enum Edward Clements
5/14/2006 11:08:01 AM
I forgot to mention that I use .Net v2.0

Edward

[quoted text, click to view]
Re: Subject: retrieving the XmlEnumAttribute values for an Enum Oleg Tkachenko [MVP]
5/15/2006 12:00:00 AM
A bit of reflection can do this:

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);
}
}

--
Oleg Tkachenko [XML MVP, MCAD]
http://www.XmlLab.Net | http://www.XLinq.Net | http://blog.tkachenko.com

[quoted text, click to view]
Re: Subject: retrieving the XmlEnumAttribute values for an Enum Edward Clements
5/16/2006 6:46:03 AM
Oleg,

Thanks for the idea and especially for the code sample; it works quite well!

Only, I couldn't find any way to connect the enumeration (int) value to the
XML string value -- for example, if the user selects "cm/a" from the combo
box, how would the program know that it should use velocityUom.ms? I could
use just a counter in the foreach() loop, but
a) would that work ok?
b) that would not take care of the situation where the enum values are not
sequential starting from zero

Could you please help?

Thanks,

Edward Clements

[quoted text, click to view]
Re: Subject: retrieving the XmlEnumAttribute values for an Enum Edward Clements
5/17/2006 6:21:02 AM
Oleg,

Sorry, I didn't make myself clear -- what I'd like to know is, while looping
through the FieldInfo to get the string XMLEnumAttributes, whether it is
possible to retrieve the corresponding enumerated (int) value for each
string; I could use just a counter in the foreach() loop, but
a) would that work ok?
b) that would not take care of the situation where the enum values are not
sequential starting from zero

Thanks,

Edward Clements


[quoted text, click to view]
Re: Subject: retrieving the XmlEnumAttribute values for an Enum Oleg Tkachenko [MVP]
5/17/2006 3:31:21 PM
[quoted text, click to view]

Well, combobox items have both text and value behind. That's your
responsibility to make sure that with "cm/a" item in a combobox has
velocityUom.ms value.

--
Oleg Tkachenko [XML MVP, MCAD]
AddThis Social Bookmark Button