Wednesday, 18 July 2012

Accessing XML Data using WebMatrix

Although I’ve been a .NET developer for around a decade, the majority of my work has not been on websites, so it is only occasionally that I get to dabble in ASP.NET. However, I do try to keep up with the new technologies by building a few small websites from time to time. Recently, I decided to try learning a bit of Azure and Razor syntax by porting the first ASP.NET site I made ages ago over to WebMatrix. I’m using the release candidate of WebMatrix 2.

The website I am porting did a very simple job. It displayed football fixtures and results from XML files I kept up to date manually. It could also keep a running total of the top scorers for the season. The main challenge in porting it was getting to grips with the Razor syntax, although how to access XML data was not something that was immediately obvious to me. Of course, it’s simple once you know how, but here’s the steps:

1. Put your XML files into App_Data

Just copy them into the folder (create App_Data yourself if it doesn’t exist)

2. Reference the System.Xml.Linq namespace

This can be done at the top of your Razor cshtml file with the @using keyword:

@using System.Xml.Linq

3. Load the XDocument

Loading the XDocument is straightforward enough, but you also need to use Server.MapPath to get the real path to your XML file. I have a different XML file for each season, so I used the query string to specify which one, which you can get at using the Request object:

@{
    var season = Request["season"] ?? "2003-2004";
    var file = XDocument.Load(Server.MapPath(@"/App_Data/Fixtures" + season + ".xml"));
}

4. Perform a Query

This is straightforward LINQ to XML. I used a strongly typed class called Fixture, which I put into a C# file in the App_Code folder (you have to create this yourself).

var fixtures = from e in file.Root.Elements()
select new Fixture { 
    Competition = e.Element("Competition").Value, 
    Date = e.Element("Date").Value,
    Venue = e.Element("Venue").Value,
    Opponents = e.Element("Opponents").Value,
    Result = e.Element("Result").Value,
    For = e.Element("For").Value,
    Against = e.Element("Against").Value,
    Goalscorers = e.Element("Goalscorers").Value,
};

5. Iterate it using foreach

For some reason it took me a few goes to work out how you did a foreach in Razor. It’s actually a very clean and simple syntax:

<tbody>
@foreach (var f in fixtures) {
    <tr>
        <td>@f.Competition</td>
        <td>@f.Date</td>
        <td>@f.Venue</td>
        <td>@f.Opponents</td>
        <td>@f.Result</td>
        <td>@f.For</td>
        <td>@f.Against</td>
        <td>@f.Goalscorers</td>
    </tr>
}
</tbody>    

And that’s it. Probably painfully obvious if you are a seasoned ASP.NET developer. I’m hoping to try deploying this to Azure as well as experiment with moving the data into an sdf file, so I might blog about how easy that is to do.

Post a Comment