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);
}