简体   繁体   中英

.NET - Mapping more than one string value to XmlEnumAttribute

I have an enum that is trying to parse a string as an enum value, which works for many situations, but I started getting SOAP values that tried to resolve the same type of one of my enums, but the casing of the string was different, so I made a quick solution like this:

public enum RepoType
{

    /// <remarks/>
    local,

    /// <remarks/>
    central,

    /// <remarks/>
    [System.Xml.Serialization.XmlEnumAttribute("secure central")]        
    securecentral,

    /// <remarks/>
    [System.Xml.Serialization.XmlEnumAttribute("Secure central")]
    Securecentral,

    /// <remarks/>
    profiler,
}

Note how there are two types called Secure central, but sometimes I receive "secure central" and sometimes I receive "Secure central".

My question is there a simplified way or a unified way to handle this situation?

Thanks in advance.

If anyone else had the same trouble with this. I've solved it by adding a class level attribute [Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))] and adding property attribute to individual fields [EnumMember(Value = "secure central")]

See my solution for the OP below. There are more attributes added, just remove the ones you don't need.

[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
[System.SerializableAttribute()]

public enum RepoType
{

    /// <remarks/>
    local,

    /// <remarks/>
    central,

    /// <remarks/>
    [System.Xml.Serialization.XmlEnumAttribute("secure central")]    
    [EnumMember(Value = "secure central")]    
    securecentral,

    /// <remarks/>
    [System.Xml.Serialization.XmlEnumAttribute("Secure central")]
    [EnumMember(Value = "Secure central")]
    Securecentral,

    /// <remarks/>
    profiler,
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM