Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / XML / October 2005

Tip: Looking for answers? Try searching our database.

Matching xsd:enumeration values with spaces in classes

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Sergey Poberezovskiy - 31 Oct 2005 02:38 GMT
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.
Sergey Poberezovskiy - 31 Oct 2005 04:26 GMT
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.Serialization.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.enumerableValue);

//   ***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.

> Hi,
>
[quoted text clipped - 22 lines]
>
> Any help is really appreciated.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.