我有一个 XMLDocument 需要读取并转换为一组对象。我有以下对象
public class Location
{
public string Name;
public List<Building> Buildings;
}
public class Building
{
public string Name;
public List<Room> Rooms;
}
我有以下 XML 文件:
<?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>
这样做的最佳方法是什么?我应该自动将 xmldocument 序列化为对象,还是需要解析每个元素并手动转换为我的对象?特别是,我试图弄清楚如何转换集合(位置、建筑物等)。
将此 XML 文件基本上转换为
List<Location>
对象?