Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
238 views
in Technique[技术] by (71.8m points)

c# - What is the easiest way to convert this XML document to my object?

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;
}

and i have the following XML file:

 <?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).

What is the best suggestion to convert this XML file into basically a

List<Location>

objects?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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>

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; }
}

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
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...