What is the easiest way to convert this XML document to my object?

I have an XMLDocument that I need to read and convert to a collection of objects. I have the following objects

public class Location { public string Name; public List<Building> Buildings; } public class Building { public string Name; public List<Room> Rooms; } 

and I have the following XML file:

  <?xml version="1.0" encoding="utf-8" ?> <info> <locations> <location name="New York"> <Building name="Building1"> <Rooms> <Room name="Room1"> <Capacity>18</Capacity> </Room> <Room name="Room2"> <Capacity>6</Capacity> </Room> </Rooms> </Building> <Building name="Building2"> <Rooms> <Room name="RoomA"> <Capacity>18</Capacity> </Room> </Rooms> </Building> </location> <location name ="London"> <Building name="Building45"> <Rooms> <Room name="Room5"> <Capacity>6</Capacity> </Room> </Building> </location> </locations> </info> 

What is the best way to do this? Should I automatically serialize xmldocument to an object, or do I need to parse each element and convert it to my object manually? In particular, I am trying to figure out how to convert collections (locations, buildings, etc.).

What is the best suggestion for converting this xml file basically

 List<Location> 

objects?

+6
source share
2 answers

You can start by fixing your XML, because in the example you showed you have private tags. You can also wrap <Building> tags in a <Building> collection to have other properties in this Location class other than buildings.

 <?xml version="1.0" encoding="utf-8" ?> <info> <locations> <location name="New York"> <Buildings> <Building name="Building1"> <Rooms> <Room name="Room1"> <Capacity>18</Capacity> </Room> <Room name="Room2"> <Capacity>6</Capacity> </Room> </Rooms> </Building> <Building name="Building2"> <Rooms> <Room name="RoomA"> <Capacity>18</Capacity> </Room> </Rooms> </Building> </Buildings> </location> <location name="London"> <Buildings> <Building name="Building45"> <Rooms> <Room name="Room5"> <Capacity>6</Capacity> </Room> </Rooms> </Building> </Buildings> </location> </locations> </info> 

Once you have corrected your XML, you could adapt your models. I would recommend you use properties instead of fields in your classes:

 public class Location { [XmlAttribute("name")] public string Name { get; set; } public List<Building> Buildings { get; set; } } public class Building { [XmlAttribute("name")] public string Name { get; set; } public List<Room> Rooms { get; set; } } public class Room { [XmlAttribute("name")] public string Name { get; set; } public int Capacity { get; set; } } [XmlRoot("info")] public class Info { [XmlArray("locations")] [XmlArrayItem("location")] public List<Location> Locations { get; set; } } 

and now all that's left is deserializing the XML:

 var serializer = new XmlSerializer(typeof(Info)); using (var reader = XmlReader.Create("locations.xml")) { Info info = (Info)serializer.Deserialize(reader); List<Location> locations = info.Locations; // do whatever you wanted to do with those locations } 
+11
source

Just use the XML serialization attributes - for example:

 public class Location { [XmlAttribute("name"); public string Name; public List<Building> Buildings; } public class Building { [XmlAttribute("name"); public string Name; public List<Room> Rooms; } 

Just remember - everything will be serialized in different ways as XML elements - with the same names as the names of the objects :)

Do this to download:

 using(var stream = File.OpenRead("somefile.xml")) { var serializer = new XmlSerializer(typeof(List<Location>)); var locations = (List<Location>)serializer.Deserialize(stream ); } 
+6
source

All Articles