Question

I have an enum property. I want the serialized XML for this property to be the splitted camel-case string of the enum and vice versa.

I have two functions, one is ConcatCamelCase and the other is SplitCamelCase, I want the serializer to use them accordingly, is this possible by just decorating the field with an attribute?

If no, what are the other option without having to mess with all the other fields?

Was it helpful?

Solution

You'll have to do something like this:

public class SomeClass {
    [XmlIgnore]
    public MyEnum MyRealProperty {get;set;}

    [XmlElement("MyRealProperty")]
    [EditorBrowsable(EditorBrowsableState.Never), Browsable(false)]
    public string MyProxyProperty
    {
        get {return SplitCamelCase(MyRealProperty);}
        set {MyRealProperty = ConcatCamelCase(value);}
    }
}

OTHER TIPS

You can explicitly set the name of everything that is serialized using the XMlSerialization attributes.

[XmlRoot("theNameYouWant")]

[XmlElement("theNameYouWant")]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top