Airport data – Part 2 – Retrieveing XML from URL


In my post from Friday, I have been playing around with the Avinor data service. They state on their web site, that any use of this data should not be directed from the app user. Instead the data should be stored in a temporary database where the app. I have not thought much of the database design until now, but I have created a repository to read the XML feeds from Avior.

I have used LINQ to XML for reading the request URL and populate the data. First we need a DataContract that shall represent flight status. This class has data members for kode, English text and Norwegian text.

[DataContract(Name = "flightStatus", Namespace = "http://flydata.avinor.no")]
public class FlightStatus
{
    [DataMember(Name = "code")]
    public string Code { get; set; }

    [DataMember(Name = "statusTextEn")]
    public string EnglishText { get; set; }

    [DataMember(Name = "statusTextNo")]
    public string NorwegianText { get; set; }
}

To read the request stream with the XDocument.Load Method. After the XML is loaded, all flightStatus nodes are selected in a LINQ Query and returned in a List Collection.

public static List GetFlightStatuses(string url)
{
    var xDoc = XDocument.Load(url);
    var list = xDoc.Descendants("flightStatus")
                .Select(o => new FlightStatus()
                {
                    Code = (string)o.Attribute("code"),
                    EnglishText = (string)o.Attribute("statusTextEn"),
                    NorwegianText = (string)o.Attribute("statusTextNo"),
                })
                .ToList();
    return list;
}

Similar code is used to retieve airport and airline information. The flight information is more complex mandatory and optional members in the result set.

public static List GetFlights(string url)
{
    var xDoc = XDocument.Load(url);
    var list = xDoc.Descendants("flight")
                .Select(o => new Flight()
                {
                    Id = (string)o.Attribute("uniqueID"),
                    FlightId = GetElement(o, "flight_id"),
                    Area = GetElement(o, "dom_int"),
                    ScheduleTime = GetElement(o, "schedule_time"),
                    Direction = GetElement(o, "arr_dep"),
                    Airport = GetElement(o, "airport"),
                    Airline = GetElement(o, "airline"),
                    ViaAirports = GetElement(o, "via_airport"),
                    CheckinCounter = GetElement(o, "check_in"),
                    Gate = GetElement(o, "gate"),
                    StatusCode = GetAttribute(o, "status", "code"),
                    StatusTime = GetAttribute(o, "status", "time"),
                    LuggageBelt =  GetElement(o, "belt"),
                    Deplayed = GetElement(o, "delayed")
                })
                .ToList();
    return list;
}
private static string GetElement(XElement root, string name)
{
    if (root == null) return "";
    var obj = root.Element(name);
    return obj != null ? obj.Value : "";
}

private static string GetAttribute(XElement root, string elem, string name)
{
    if (root == null) return "";
    var obj = root.Element(elem);
    if (obj == null) return "";
    var attr = (string)obj.Attribute(name);
    return attr;
}

Happy flight data coding…

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s