将此XML文档转换为我的对象的最简单方法是什么?

时间:2013-02-16 16:30:09

标签: c# xml converter xmldocument

我有一个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>

物体

2 个答案:

答案 0 :(得分:11)

您可以从修复XML开始,因为在您显示的示例中,您有未关闭的标记。您还可以将<Building>标记包装到<Buildings>集合中,以便除了建筑物之外,还可以在此Location类中包含其他属性。

<?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后,您可以调整模型。我建议您使用属性而不是类中的字段:

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:

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
}

答案 1 :(得分:6)

只需使用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;
}

请记住 - 默认情况下,所有内容都将序列化为XML元素 - 与对象的名称相同:)

执行此操作以加载:

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