I think I found a solution:
The following code allows to convert .Net abbreviated enumerable values into
their original format. Hope this will help someone.
// *** code start ***
using System;
using System.Collections;
public class xmlEnumAttributes
{
private ArrayList _abbrevs = new ArrayList();
private ArrayList _values = new ArrayList();
public xmlEnumAttributes(Type enumType)
{
if (!enumType.IsEnum)
{
throw new ArgumentException("Not enumerable type.");
}
foreach (System.Reflection.MemberInfo info in enumType.GetMembers())
{
// I think we can safely assume only one XmlEnumAttribute per enum value
foreach (System.Xml.Serialization.XmlEnumAttribute att in
info.GetCustomAttributes(typeof(System.Xml.Seriali zation.XmlEnumAttribute),
true))
{
_abbrevs.Add(info.Name);
_values.Add(att.Name);
break;
}
}
}
public string GetValue(object abbrevValue)
{
string abbrev = abbrevValue.ToString();
int index = _abbrevs.IndexOf(_abbrevs, abbrev);
return (index > -1) ? _values[index] : null;
}
}
// example of using the class:
// if myXmlDeserialisedObject.enumerableValue is of myEnum type
xmlEnumAttributes myEnumValues = new xmlEnumAttributes(typeof(myEnum));
string value = myEnumValues.GetValue(myXmlDeserialisedObject.enum erableValue);
// ***end of code segment ***
PS I have only used string enumerated values and therefore the code for
other enumerated types may require different code. Also If anyone could
specify a better solution to the problem in my original post, I would really
appreciate that.
"Sergey Poberezovskiy" wrote:
Hi,
I have a simple enumeration in my schema:
<xs:element name="el_1">
<xs:simpleType>
<xs:restiction base="xs:string">
<xs:enumeration value="value and space 1"/>
<xs:enumeration value="value2 with spaces"/>
...
</xs:restiction>
</xs:simpleType>
<xs:element>
.Net 2003 xsd.exe utility generates a corresponding enumeration without any
spaces (all commas, full stops, etc. are also removed). Now I need to match
the value of the element with one stored in my reference table in the
database, but because the value that comes from the generated class has all
non-alphanumeric characters removed - I am unable to match it.
I noticed that every enumerated value has a custom
(Xml.Serialization.XmlEnumAttribute) attribute assigned to it with the actual
spelling that appears in the document. But I do not know how to retrieve this
attribute from the enumerated value I receive from the class.
Any help is really appreciated.