I need to generate a List<Event>
from a XML document, and the Event
has a List<Contact>
. How can I convert the XML to a List<Event>
object when reading:
This is my event class:
public class Event
{
public string id { get; set; }
public string title { get; set; }
public DateTime Start { get; set; }
public DateTime End { get; set; }
public virtual List<Contact> contacts { get; set; }
}
Contact class
public class Contact
{
public int Id { get; set; }
public string Name { get; set; }
}
This is how I save the XML document when the user adds something.
<Event>
<id>1</id>
<title>AA</title>
<start>2019-12-01T14:13:58.863</start>
<end>2019-12-01T15:13:58.787</end>
<contacts>
<contact>
<id>1</id>
<name>ABC</name>
</contact>
<contact>
<id>2</id>
<name>ABCD</name>
</contact>
<contact>
<id>3</id>
<name>ABCDE</name>
</contact>
</contacts>
</Event>
To get the XML document I use
XDocument xml = new XDocument();
xmlDoc = XDocument.Load("Data.xml");
I'm struck on how to read the items to a list. How can I do it?