将此XML文档转换为我的对象的最简单方法是什么? [英] What is the easiest way to convert this XML document to my object?
问题描述
我有一个XMLDocument,需要读入并转换为一组对象。我有以下对象
I have an XMLDocument that i need to read in and convert into a set 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;
}
我有以下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序列化为对象,还是需要解析每个元素并手动将其转换为对象?特别是,我试图找出如何转换集合(位置,建筑物等)。
What is the best way of doing this? Should I be serializing the xmldocument to the object automatically or do i need to parse out each element and convert into my object manually? In particular, I am trying to figure out how to convert the collections (locations, buildings, etc).
将这个XML文件基本上转换为a的最佳建议是什么
What is the best suggestion to convert this XML file into basically a
List<Location>
对象?
推荐答案
您可以先修复XML,因为在示例中,您显示了未关闭的标签。您也可以将< Building>
标记包装到< Buildings>
集合中,以便具有
You could start by fixing your XML because in the example you have shown you have unclosed tags. You might also wrap the <Building>
tags into a <Buildings>
collection in order to be able 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>
一旦修复了XML,就可以修改模型。我建议您在类中使用属性而不是字段:
Once you have fixed your XML you could adapt your models. I would recommend you using 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; }
}
现在剩下的就是反序列化XML:
and now all that's left is deserialize 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
}
这篇关于将此XML文档转换为我的对象的最简单方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!