ASP.NET charts example: Odin Sphere: Part 2 - Parsing the XML

In part one of this series we covered what we'd be doing, and what model we'd be using for the data.

This time we'll parse the XML file that contains the data we need, and populate the objects.

Loading the XML file

The XML file we'll be loading is located at http://jamesrskemp.com/files/OdinSphere.xml, and to keep it simple, we'll load it in assuming we're on a different server/domain.

First we'll need to add the following so we can make use of XDocument.

using System.Xml.Linq;

Next we'll update ProcessRequest by loading the XML file.

XDocument dataFile = XDocument.Load("http://jamesrskemp.com/files/OdinSphere.xml");

And then we'll parse it out into our custom objects.

IEnumerable<Character> characterData = from characters in dataFile.Descendants("Character")
    select new Character {
        Name = characters.Attribute("name").Value,
        HpLevels = (from levels in characters.Element("HP").Element("Levels").Descendants("Level")
            select new HpLevel {
                Level = int.Parse(levels.Attribute("id").Value),
                HitPoints = int.Parse(levels.Attribute("hitPoints").Value)
            }
        ).ToList()
    };

With that done, we can now verify the data by displaying some very basic information on the page.

     context.Response.ContentType = "text/plain";
        foreach (Character character in characterData) {
            context.Response.Write(character.Name + Environment.NewLine);
            context.Response.Write("Maximum HP level = " + character.HpLevels.Last().Level.ToString() + Environment.NewLine + Environment.NewLine);

        }

With that aspect verified, we can create and output our graphs, which we'll cover in part three.