I've been wondering if it's possible to do this.
It would be a great help in cases where an XML response has incorrect values that are needing to be mapped to enums.
The case I'm dealing with specifically is when an expected value has a trailing space and the enum is expecting it without.
XML:
<Foo>
<Bar>EnumValue </Bar>
</Foo>
Enum:
public enum MyEnum
{
[XmlEnum("EnumValue")]
EnumValue
}
Class:
public class Foo
{
[XmlElement("Bar")]
public MyEnum myEnum { get; set; }
}
I've investigated using a custom attribute (instead of "XmlEnum") to trim the values but it doesn't seem to be reached during the deserialization.
Is there a way to trim XML values (when needed) before/during deserialization so that the value can be mapped to the enum correctly?
-
I should add that I can't make any changes to how the XML is sent, I can only deal with the response.
Also, simply changing the attribute parameter to [XmlEnum("EnumValue ")] fixes the issue, but this is not satisfactory as the XML value could be altered at a later date.