I have an XMLDocument that i need to read in and convert into a set of objects. I have the following objects
我有一个XMLDocument,需要读取它并将其转换为一组对象。我有以下的对象
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文件:
<?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 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).
最好的方法是什么?我应该自动将xmldocument序列化为对象,还是需要解析每个元素并手工转换为对象?特别是,我正在设法弄清楚如何转换集合(位置、建筑物等)。
What is the best suggestion to convert this XML file into basically a
将这个XML文件转换为a的最佳建议是什么
List<Location>
objects?
对象?
2 个解决方案
#1
11
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开始,因为在示例中显示了未关闭的标记。您还可以将 <构建> 标记放入 <建筑物> 集合中,以便能够在此位置类中拥有其他属性,而不是建筑物。
<?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 fixed your XML you could adapt your models. I would recommend you using properties instead of fields in your classes:
一旦修复了XML,就可以调整模型。我建议您在您的类中使用属性而不是字段:
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 deserialize the XML:
现在剩下的就是反序列化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
}
#2
6
Just use the XML serialization attributes- for example:
只需使用XML序列化属性—例如:
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 as XML Elements by default - with the sames the same as the names of the objects :)
只要记住——一切都将被序列化为XML元素——sames的名称与对象的名称相同:)
Do this to load:
做这个加载:
using(var stream = File.OpenRead("somefile.xml"))
{
var serializer = new XmlSerializer(typeof(List<Location>));
var locations = (List<Location>)serializer.Deserialize(stream );
}
#1
11
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开始,因为在示例中显示了未关闭的标记。您还可以将 <构建> 标记放入 <建筑物> 集合中,以便能够在此位置类中拥有其他属性,而不是建筑物。
<?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 fixed your XML you could adapt your models. I would recommend you using properties instead of fields in your classes:
一旦修复了XML,就可以调整模型。我建议您在您的类中使用属性而不是字段:
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 deserialize the XML:
现在剩下的就是反序列化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
}
#2
6
Just use the XML serialization attributes- for example:
只需使用XML序列化属性—例如:
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 as XML Elements by default - with the sames the same as the names of the objects :)
只要记住——一切都将被序列化为XML元素——sames的名称与对象的名称相同:)
Do this to load:
做这个加载:
using(var stream = File.OpenRead("somefile.xml"))
{
var serializer = new XmlSerializer(typeof(List<Location>));
var locations = (List<Location>)serializer.Deserialize(stream );
}