Deserializing nested XML into classes

francisand

New Member
I have the following XML:\[code\]<Root> <EventSet> <Event> <id>12345</id> <rant> <localTime> <dst>true</dst> <time>7/2/2012 14:30</time> </localtime> <randomRant> <random>to illustrate point</random> <rant>help me!</rant> </randomRant> </rant> </Event> <Event>/*another event*/</Event> <Event>/*another event*/</Event> //etc </EventSet></Root>\[/code\]I want to map this to:\[code\][Serializable] public class Events { public List<Event> events { get; set; } } [Serializable] public class Event { public int id { get; set; } public Rant rant { get; set; } //this is where I get confused}\[/code\]QUESTION: How do I serialize the tags within \[code\]<rant>\[/code\]? Do I continue to make serialized classes of the parent until the child tags have no children? For example, below:\[code\][Serializable] public class Rant{ public LocalTime localTime { get; set; } public RandomRant randomRant { get; set; }}[Serializable] public class LocalTime{ public bool dst { get; set; } public DateTime time { get; set; }}[Serializable] public class RandomRant{ public string random { get; set; } public string rant { get; set; }}\[/code\]Or is there a better approach?EDIT: A given \[code\]event\[/code\] has one and only one \[code\]id\[/code\], and one and only one \[code\]rant\[/code\]. For the sake of my question, assume my types are valid. I am looking to deserialize nested XML into an object.
 
Back
Top