- Lynne Harding
- Posts : 335
Join date : 2018-12-20
Strange Serialization Problem
Fri 24 Jan 2020 - 8:57
I'm Deserializing some data in a class which contains an Enum, it serializes the value fine but doesn't pick it up again when you deserialize.
[Serializable]
public enum eTelephoneType
{
[XmlEnum(“Home”)]
Home,
[XmlEnum(“Work”)]
Work,
[XmlEnum(“Mobile”)]
Mobile,
[XmlEnum(“Other”)]
Other,
[XmlEnum(““)
] Unknown }
Partial Class Definitions
[XmlElement(“TelephoneNumbers”)]
public List<TelephoneNumber> TelephoneNumbers { get; set; }
[Serializable]
public class TelephoneNumber
{
public TelephoneNumber() { }
[XmlElement(“TelephoneNumber”)]
public string Number { get; set; }
[XmlAttribute(“Type”)]
public eTelephoneType Type { get; set; }
[XmlAttribute(“ContactTimes”)]
public eContactTimes ContactTimes { get; set; }
}
XML Example
<TelephoneNumbers>
<TelephoneNumber Type=“Work”>01234 101022</TelephoneNumber>
<TelephoneNumbers>
Event with all the Xml attributes on the classes it still doesn't pick up Type = Work and see the class value to eTelephoneType.Work. Anyone spot anything I've missed or am I looking at writing some custom serializion for this?
[Serializable]
public enum eTelephoneType
{
[XmlEnum(“Home”)]
Home,
[XmlEnum(“Work”)]
Work,
[XmlEnum(“Mobile”)]
Mobile,
[XmlEnum(“Other”)]
Other,
[XmlEnum(““)
] Unknown }
Partial Class Definitions
[XmlElement(“TelephoneNumbers”)]
public List<TelephoneNumber> TelephoneNumbers { get; set; }
[Serializable]
public class TelephoneNumber
{
public TelephoneNumber() { }
[XmlElement(“TelephoneNumber”)]
public string Number { get; set; }
[XmlAttribute(“Type”)]
public eTelephoneType Type { get; set; }
[XmlAttribute(“ContactTimes”)]
public eContactTimes ContactTimes { get; set; }
}
XML Example
<TelephoneNumbers>
<TelephoneNumber Type=“Work”>01234 101022</TelephoneNumber>
<TelephoneNumbers>
Event with all the Xml attributes on the classes it still doesn't pick up Type = Work and see the class value to eTelephoneType.Work. Anyone spot anything I've missed or am I looking at writing some custom serializion for this?
- Lynne Harding
- Posts : 335
Join date : 2018-12-20
Re: Strange Serialization Problem
Fri 24 Jan 2020 - 8:59
I don't think your XML matches your class. I think you need to remove [XmlElement(“TelephoneNumber”)] from the number property and replace it with [XmlText()] because in your XML the number is the text value and in your class as it currentlystands it will be expecting an element called TelephoneNumber inside an element called TelephoneNumber.
eg. <TelephoneNumber Type=“Mobile”><TelephoneNumber>123</TelephoneNumber></TelephoneNumber>
eg. <TelephoneNumber Type=“Mobile”><TelephoneNumber>123</TelephoneNumber></TelephoneNumber>
Permissions in this forum:
You cannot reply to topics in this forum
|
|