什么是这个XML文档转换为我的对象最简单的方法?(What is the easiest way t

2019-07-20 00:45发布

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

对象?

Answer 1:

因为在例子中,你表现出你有未关闭的标签,你可以通过固定的XML开始。 您可能还包裹<Building>标记为<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,你可以调整你的模型。 我将使用属性,而不是在你的类领域建议你:

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
}


Answer 2:

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


文章来源: What is the easiest way to convert this XML document to my object?