I'm trying to serialize Xml using the XmlSerializer and by using its attributes. My problem is that I'm not getting the data of the EVENT items. I'm not fully aware of how to treat the combination between EVENTSCOLLECTION and EVENT by using the attributes (XmlRoot and XmlElement - should I use XmlArray ?) I'm getting the EventsCollection with 0 items. I would rather get rid of the EventsCollection class - it is there just because I don't know how to use the combination. This is my XML:
<MSGDATA>
<EVENTSCOLLECTION>
<EVENT>
<Id>1</Id>
<EventNumber>100</EventNumber>
</EVENT>
<EVENT>
<Id>2</Id>
<EventNumber>200</EventNumber>
</EVENT>
<EVENT>
<Id>3</Id>
<EventNumber>300</EventNumber>
</EVENT>
</EVENTSCOLLECTION>
</MSGDATA>
This is the data structure:
[XmlRoot(ElementName="EVENT")]
public class EventItem
{
[XmlElement("Id")]
public int ID {get; set;}
[XmlElement("EventNumber")]
public int EventNumber {get; set;}
}
//I would get rid of it if I could find a way
[XmlRoot(ElementName="EVENTSCOLLECTION")]
public class EventsCollection
{
private List<EventItem> eventItems
public List<EventItem> EventItems
{
get
{
if (eventItems == null)
{
eventItems = new List<EventItem>();
}
return eventItems;
}
set
{
eventItems = value;
}
}
}
[XmlRoot(ElementName="MSGDATA")]
public class EventMsgData
{
[XmlElement("EVENTSCOLLECTION")]
//I would replace this propery with a property with a type of List<EventItem> if I would know how to combine it with the XML attributes
private EventsCollection events;
public EventsCollection Events { get; set; }
}
This is How I try to serialize it:
XmlSerializer xmls = new XmlSerialized(typeof(EventMsgData));
EventMsgData result = (EventMsgData)xmls.Deserialize(new StringReader(<text of MSGDATA xml node>))