0

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?

Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632

2 Answers2

1

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);}
    }
}
Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • Sounds fair. But is there a way to override some sort of the XML attributes to make it work without a proxy prop? I have many properties and I prefer not to create proxies for all of them if possible. Thanks John. – Shimmy Weitzhandler Oct 27 '11 at 09:43
  • Sorry, no way to do what you want. How are your classes created? By hand, or are they generated? If the latter, then generate them to this pattern. – John Saunders Oct 27 '11 at 09:44
  • I have many enum fields and I guess I will have to create proxies for each and one of them, I thought there is an attribute that I can provide with a function to be performed on field, like there is in [`DataAnnotations.CustomValidationAttribute`](http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.customvalidationattribute.aspx) :( – Shimmy Weitzhandler Oct 27 '11 at 10:20
  • No, never has been. It's not something that needs to be done often. – John Saunders Oct 27 '11 at 13:40
  • I do agree. Thanks again John. – Shimmy Weitzhandler Oct 27 '11 at 16:04
-1

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

[XmlRoot("theNameYouWant")]

[XmlElement("theNameYouWant")]
John Saunders
  • 160,644
  • 26
  • 247
  • 397
MartyTPS
  • 530
  • 2
  • 5