InfoSpunge
InfoSpunge

Reputation: 59

How to extract the data from this xml document

I want to extract the data from every planet and asteroid (not in this example) and put it in a list of strings. So one planetlist would contain (in order):

I have a lot of trouble traversing the xml's, and there's not a lot of good tutorials on this imo.

The xml:

<galaxy>
  <planet>
    <name>Kobol</name>
    <position>
      <x>45</x>
      <y>310</y>
      <radius>7</radius>
    </position>
    <speed>
      <x>0.3</x>
      <y>-0.6</y>
    </speed>
    <neighbours>
      <planet>Unicron</planet>
      <planet>Koarth</planet>
    </neighbours>
    <color>blue</color>
    <oncollision>blink</oncollision>
  </planet>
  <planet>
    <name>Namek</name>
    <position>
      <x>60</x>
      <y>102</y>
      <radius>15</radius>
    </position>
    <speed>
      <x>0.1</x>
      <y>0.2</y>
    </speed>
    <neighbours>
      <planet>Helicon</planet>
      <planet>Synnax</planet>
      <planet>Xenex</planet>
      <planet>Alderaan</planet>
    </neighbours>
    <color>orange</color>
    <oncollision>blink</oncollision>
  </planet>
</galaxy>

Can anyone help?

Upvotes: 1

Views: 445

Answers (2)

Hayden
Hayden

Reputation: 2988

We can define models that represent the Xml in the following way:

[XmlRoot("galaxy")]
public class Galaxy
{
    [XmlElement("planet")]
    public List<Planet> Planets { get; set; }
}

public class Planet
{
    [XmlElement("name")]
    public string Name { get; set; }
    
    [XmlElement("position")]
    public Position Position { get; set; }
    
    [XmlElement("speed")]
    public Speed Speed { get; set; }
    
    [XmlElement("neighbours")]
    public List<Neighbour> Neighbours { get; set; }
    
    [XmlElement("color")]
    public string Color { get; set; }
    
    [XmlElement("oncollision")]
    public string OnCollision { get; set; }
}

public class Position
{
    [XmlElement("x")]
    public double X { get; set; }

    [XmlElement("y")]
    public double Y { get; set; }

    [XmlElement("radius")]
    public double Radius { get; set; }
}

public class Speed
{
    [XmlElement("x")]
    public double X { get; set; }

    [XmlElement("y")]
    public double Y { get; set; }
}

public class Neighbour
{
    [XmlElement("planet")]
    public string Name { get; set; }
}

Note how we're using the attributes to define the layout of the Xml, and map it back to the object.

With this, we're able to deserialize the Xml in the following way:

XmlSerializer serializer = new XmlSerializer(typeof(Galaxy));

using (FileStream stream = new FileStream(filePath, FileMode.Open))
{
    Galaxy galaxy = (Galaxy) serializer.Deserialize(stream);

    foreach (Planet planet in galaxy.Planets)
    {
        Console.WriteLine(planet.Name);
    }
}

Output

Kobol
Namek

Upvotes: 1

Jonathan
Jonathan

Reputation: 5018

What you're talking about is XML Deserialization. Going the other way (object to xml) is called Serialization

There are plenty of examples of both of these on StackOverflow and the web in general. Searching with these terms should help you.

Here's a good one from StackOverflow:

How to Deserialize XML document

Upvotes: 2

Related Questions