3

I've refactored code like this:

public string CamelCASE { get; set; }

to:

public string CamelCase {get; set; }

only do discover that the input XML contains the former casing (let's call it a shouting camel). I have no control over how the XML document is produced. Nor do I burn of desire to retract my changes.

I'd like to map the loud camel property to a softly speaking one.

I've tried XmlElement and XmlMapping but to no greater success. A googling gave me only hits on how to map stuff to attributes, along lines of this post. However, I need only something like <LoudCAMEL> to be deserialized to a property public string QuietCamel.

Is there a smooth way to do so?

Edit

After adding the attribute as follows:

using System.Collections.Generic;
using System.Xml;

public class Beep : SuperBeep
{
  private readonly BeepType _a;

  public Beep() { _a = BeepType.SomeSome; }
  public Beep(BeepType input) { _a = input; }
  ~Beep() { }
  public override void Dispose() { }

  public BeepType Aaa { get { return _a; } }

  [XmlElement("CamelCASE")]
  public bool CamelCase { get; set; }
}

I can see the red, wavy highlight telling me Cannot access constructor 'XmlElement' here due its protection level. When I compile, though, I get the IDE crying out loud that 'System.Xml.XmlElement' is not an attribute class.

Frankly, I'm a bit confused by the suggestion to use attributes (this is targeting .NET 2.0), since I was under the impression that attributing wasn't available to .NET prior to version 3.5. Am I mistaken?

Community
  • 1
  • 1
Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438
  • 1
    Re the edit: try changing it to `[XmlElementAttribute("CamelCASE")]` - semantically identical, but it might make the 2.0 compiler happy – Marc Gravell Jul 22 '14 at 07:07
  • Alternatively, double-check that you have `using System.Xml.Serialization;` at the top of the file – Marc Gravell Jul 22 '14 at 07:08

2 Answers2

5
[XmlElement("CamelCASE")]
public string CamelCase { get; set; }

should be all you need, if you are keeping the shouty name in the xml. If you want to use the quieter name in new xml, but allow the old name to still work, it gets more complicated. You could use:

public string CamelCase { get; set; }

[XmlElement("CamelCASE"), Browsable(false)]
[EditorBrowsable(EditorBrowsableState.Never)]
public string CamelCaseLegacy {
    get { return CamelCase; }
    set { CamelCase = value; }
}
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public bool ShouldSerializeCamelCaseLegacy() { return false; }

When serializing, the CamelCase property will serialize to <CamelCase>, and the CamelCaseLegacy element will be ignored due to the ShouldSerialize* method. However, when deserializing, the CamelCaseLegacy property will be used whenever <CamelCASE> is seen. We then map this value back to the CamelCase property.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • +1 for completeness. Luckily, in my case, re-camelization won't be necessary. – Konrad Viltersten Jul 21 '14 at 12:46
  • Woah, I was a bit too quick to jump of joy. I just noticed that *XmlElement* caused a problem. Apparently, the project I'm working in is .NET 2.0. There are no attributes prior .NET 3.0 if I recall correctly. What to do then?! – Konrad Viltersten Jul 21 '14 at 12:52
  • @KonradViltersten that attribute has existed since 1.1; it should be there: http://msdn.microsoft.com/en-us/library/aa335742(v=vs.71).aspx – Marc Gravell Jul 21 '14 at 12:58
  • Hmm... I'm not at work right now but as far I recall I got the compiler whining about the scope of the constructor of *XmlElement* being too narrow or something. Both my class and the property in it are public, though... Weird. Suggestions? – Konrad Viltersten Jul 21 '14 at 19:28
  • @Konrad my suggestion is: post the exact code that is problematic, and the exact error mesaage – Marc Gravell Jul 21 '14 at 20:06
  • That's because I underestimated how quick you're to read my stuff. Now you can see it. :) – Konrad Viltersten Jul 22 '14 at 06:57
2

You are referring to the wrong namespace.

Remove

using System.Xml;

and add

using System.Xml.Serialization;
Haseeb Jadoon
  • 450
  • 6
  • 20