0

My project uses XSD2Code++ to generate a large amount of C# classes and enums from XSD source files. By default, the tool generates C# enums as in the following example:

From XSD definition:

<simpleType name ="AccessResultsEnum">
  <restriction base="string">
    <enumeration value="Unknown"/>
    <enumeration value="AccessAuthorized"/>
    <enumeration value="AccessNotAuthorized"/>
  </restriction>
</simpleType>

To C# Enum type:

[System.CodeDom.Compiler.GeneratedCodeAttribute("Xsd2Code", "4.2.0.31")]
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://www.mycompany.it/MyProject/StateValues/")]
public enum AccessResultsEnum
{
    /// <remarks/>
    Unknown,
    
    /// <remarks/>
    AccessAuthorized,
    
    /// <remarks/>
    AccessNotAuthorized,
}

Now, I would like to generate a C# enum with custom assigned values like the following one:

public enum AccessResultsEnum
{
    /// <remarks/>
    Unknown = 0,
    
    /// <remarks/>
    AccessAuthorized = 2,
    
    /// <remarks/>
    AccessNotAuthorized = 63,
}

Is there a way to achieve this using XSD2Code++?

I should keep enum as the target C# type but I can change the XSD AccessResultsEnum type definition.

danfic
  • 1
  • 1

1 Answers1

0

Normally I do it with a get/set

    public class Test
    {
        public enum AccessResultsEnum
        {
            /// <remarks/>
            Unknown = 0,
            /// <remarks/>
            AccessAuthorized = 2,
            /// <remarks/>
            AccessNotAuthorized = 63,
        }
        private AccessResultsEnum _AccessResultsEnum { get;set;}
        public string AccessResultsEnum {
            get{ return _AccessResultsEnum.ToString();}
            set{ _AccessResultsEnum =(AccessResultsEnum)Enum.Parse(typeof(AccessResultsEnum), value);}
        }
    }
jdweng
  • 33,250
  • 2
  • 15
  • 20
  • Thank you for your answer but this does not solve my problem. My start point is the XSD definition file that I have to update from time to time. Then from the XML I automatically generate the C# code. I am looking for a way to define the XML code such that XSD2Code generates a C# enum with customized values and not just an integer sequence as by default. This allows me to not write error-prone internal mapping code for those enums. – danfic Mar 16 '21 at 11:40
  • Isn't that what the code does? It take the string name and converts to/from the enumeration. – jdweng Mar 16 '21 at 11:51
  • I don't want to convert the enumeration type to/from its string representation. C# assigns internal integer values to enums, but you can assign custom internal int values just using the "=" operator after the item name. Well, what I want to achieve is that the automatically-generated C# enum code to have my assigned integer values for each element rather than the default sequential ones. For example, this fake XML `` should generate this C# enum item `AccessNotAuthorized = 63`. Please, let me know if I clarified what is my goal. – danfic Mar 16 '21 at 12:12
  • c# is just using the enum NAME which is a string. The only time you have to worry about the number is when you are importing/exporting to a different application that is using the number instead of the string. So using Enum.Parse() like in my code above is what is needed at the external interface. – jdweng Mar 16 '21 at 13:14
  • My project is a middleware receiving commands from frontend apps using named enums. These enums are generated by XSD2Code which produces a basic C# enum with sequential internal values starting from 0. The backend app (embedded devices) use int values. The problem is that I have to write mapping code to/from frontend and backend applications and this is an error-prone situation. So, I would like to have a way to generate C# code where enums have my internal values so I can have a 1 to 1 matching between values used by frontend and backend applications. – danfic Mar 16 '21 at 14:23
  • The middleware does not need the numeric values. You need a class object that does mapping which is loaded by the frontend and read by the backend. – jdweng Mar 16 '21 at 15:00
  • This is what I currently have but, for several reasons that are quite complex to explain here, I think having a 1:1 mapping may simplify things to people involved in the whole system life cycle. In any case, thank you very much for your help, I really aprreciated that. – danfic Mar 17 '21 at 07:18