0

i'm trying to make an Angle class. when i serialize this class to xml i want to following:

<Angle Unit="Degree">90</Angle>

or

<Angle Unit="Radian">3.14......</Angle>

currently when i serialize my class i get the following:

<Angle Unit="Degree">
  <Degree>90</Degree>
</Angle>

or

<Angle Unit="Radian">
  <Radian>90</Radian>
</Angle>  

i know it is possible for strings with [XmlText], but is there a way for double or other values without the need to make a custom xmlwrite and xmlread?

the following shows parts of my class code:

[Serializable]
public struct Angle
{
    [XmlAttribute]
    public UnitType Unit;

    public double Radian
    {
        get;
        set;
    }
    public bool ShouldSerializeRadian();

    public double Degree
    {
        get;
        set;
    }
    public bool ShouldSerializeDegree();
}

with Unit and shouldSerialize i select what value to use.

when i set degree = 90 the value for radian will be 1.5707...

UnitType is an enum with Degree and Radian. is unit = unittype.degree degree will be used when serialized and when unit = unittype.radian radain will be used when serialized.

the code i use to select what representation i use is as following:

public bool ShouldSerializeRadian() 
{ 
   return (Unit == UnitType.Radian); 
}
  • if its `Radian` will `Degree` be 0 ? – sa_ddam213 Feb 03 '13 at 23:14
  • @sa_ddam213 'UnitType' is an enum with Degree and Radian. is 'unit = unittype.degree' degree will be used when serialized and when 'unit = unittype.radian' radain will be used when serialized. 'public bool ShouldSerializeRadian() { return (Unit == UnitType.Radian); } ' so if degree = 90 radian will be 1.5707... – user2038134 Feb 03 '13 at 23:18
  • Yes, but if you set the `Angle` to `Radian`(double) will the `Degree`(double) value be 0 – sa_ddam213 Feb 03 '13 at 23:21
  • @sa_ddam213 no is i set `Angle` to `Radian` (3.14..) `Degree` will be 180 – user2038134 Feb 03 '13 at 23:29
  • this thread http://stackoverflow.com/questions/4154621/add-xml-attribute-to-string-property looks like your case. – d1mitar Feb 03 '13 at 23:36
  • @d1mitar it's more like [link](http://stackoverflow.com/questions/797055/xml-serialization-question-how-to-serialize-element-attribute-and-text-from-o) but it uses the `[XmlText]` attribute and that can only be used for strings not for double ect. – user2038134 Feb 03 '13 at 23:40

1 Answers1

1

Not sure if this is the best solution but you could make a surrogate property to handle the Serilization/Deserialization.

Something Like

[Serializable]
public class Angle 
{
    [XmlAttribute]
    public UnitType Unit;

    [XmlTextAttribute]
    public double Value
    {
        get { return Unit == UnitType.Degree ? Degree : Radian; }
        set
        {
            if (Unit == UnitType.Degree)
            {
                Degree = value;
                return;
            }
            Radian = value;
        }
    }

    [XmlIgnore]
    public double Radian { get; set; }

    [XmlIgnore]
    public double Degree { get; set; }
}

Result:

  <Angle Unit="Radian">70.8</Angle> 
  <Angle Unit="Degree">45.2</Angle>

sa_ddam213
  • 42,848
  • 7
  • 101
  • 110
  • this indeed worked and and cleaner then the shouldserialize methods to select what representation to use thanks – user2038134 Feb 03 '13 at 23:48