0

I have a client supplied wsdl file containing an enum:

<xsd:simpleType name="OurEnum">
<xsd:annotation>
  <xsd:appinfo>
    <i:Base i:namespace="http://x.com/y/structures/2.0" i:name="Object"/>
  </xsd:appinfo>
</xsd:annotation>
<xsd:restriction base="xsd:token">
  <xsd:enumeration value="0"/>
  <xsd:enumeration value="1"/>
  <xsd:enumeration value="2"/>
  <xsd:enumeration value="3"/>
  <xsd:enumeration value="10"/>
</xsd:restriction>

Using Svcutil to create the WCF client, the code for enum looks like this:

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.233")]
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://x.com/y/z/2.0")]
public enum OurEnum
{

    /// <remarks/>
    [System.Xml.Serialization.XmlEnumAttribute("0")]
    Item0,

    /// <remarks/>
    [System.Xml.Serialization.XmlEnumAttribute("1")]
    Item1,

    /// <remarks/>
    [System.Xml.Serialization.XmlEnumAttribute("10")]
    Item10,

    /// <remarks/>
    [System.Xml.Serialization.XmlEnumAttribute("2")]
    Item2,

    /// <remarks/>
    [System.Xml.Serialization.XmlEnumAttribute("3")]
    Item3,

 }

The creation of enum values as Item0, Item1, Item10 is very annoying. How should the Svcutil be used so that the generated code looks like:

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.233")]
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://x.com/y/z/2.0")]
public enum OurEnum
{

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

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

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

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

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

I have already tried using following serialization options for with Svcutil:

"/serializer:Auto" "/serializer:DataContractSerializer (failed badly)" "/serializer:XmlSerializer" "/importXmlTypes"

but the result remains the same.

I have also tried using Xsd.exe to create c-sharp code files from the xsds referred in the wsdl, but the result still remains the same.

Is there any other free tool out there, that can do the expected?

Any insight is greatly appreciated!

ProgramFOX
  • 6,131
  • 11
  • 45
  • 51
DiligentKarma
  • 5,198
  • 1
  • 30
  • 33

1 Answers1

1

I have had a work around to my problem. The idea is taken from here with due thanks and regards.

The actual problem was how to get Item10, when given a value of 10. Taking a cue from the solution cited at the above link, I came up with the following method, which when passed a value contained in XmlEnumAttribute, would return the enum value:

private static T GetEnumValueFromXmlAttrName<T>(string attribVal)
    {
        T val = default(T);

        if (typeof(T).BaseType.FullName.Equals("System.Enum"))
        {
            FieldInfo[] fields = typeof(T).GetFields();

            foreach (FieldInfo field in fields)
            {
                object[] attribs = field.GetCustomAttributes(typeof(XmlEnumAttribute), false);

                foreach (object attr in attribs)
                {
                    if ((attr as XmlEnumAttribute).Name.Equals(attribVal))
                    {
                        val = (T)field.GetValue(null);
                        return val;
                    }
                }
            }
        }
        else
            throw new Exception("The supplied type is not an Enum.");

        return val;
    }

Hope this helps!

Community
  • 1
  • 1
DiligentKarma
  • 5,198
  • 1
  • 30
  • 33