コード例 #1
0
        private static TripDetails ParseTripDetails(XElement trip)
        {
            TripDetails tripDetails = new TripDetails();

            tripDetails.tripId = SafeGetValue(trip.Element("tripId"));

            XElement statusElement;
            if (trip.Element("tripStatus") != null)
            {
                // ArrivalsForStop returns the status element as 'tripStatus'
                statusElement = trip.Element("tripStatus");
            }
            else if (trip.Element("status") != null)
            {
                // The TripDetails query returns 'status'
                statusElement = trip.Element("status");
            }
            else
            {
                // No status available, stop parsing here
                return tripDetails;
            }

            // TODO: Log a warning for when the serviceDate is invalid. This might be a OBA bug, but I don't
            // have the debugging info to prove it
            string serviceDate = SafeGetValue(statusElement.Element("serviceDate"));
            if (string.IsNullOrEmpty(serviceDate) == false)
            {
                long serviceDateLong;
                bool success = long.TryParse(serviceDate, out serviceDateLong);
                if (success)
                {
                    tripDetails.serviceDate = UnixTimeToDateTime(serviceDateLong);
                    if (string.IsNullOrEmpty(SafeGetValue(statusElement.Element("predicted"))) == false
                        && bool.Parse(SafeGetValue(statusElement.Element("predicted"))) == true)
                    {
                        tripDetails.scheduleDeviationInSec = int.Parse(SafeGetValue(statusElement.Element("scheduleDeviation")));
                        tripDetails.closestStopId = SafeGetValue(statusElement.Element("closestStop"));
                        tripDetails.closestStopTimeOffset = int.Parse(SafeGetValue(statusElement.Element("closestStopTimeOffset")));

                        if (statusElement.Element("position") != null)
                        {
                            tripDetails.location = new GeoCoordinate(
                                double.Parse(SafeGetValue(statusElement.Element("position").Element("lat")), NumberFormatInfo.InvariantInfo),
                                double.Parse(SafeGetValue(statusElement.Element("position").Element("lon")), NumberFormatInfo.InvariantInfo)
                                );
                        }
                    }
                }
            }

            return tripDetails;
        }
コード例 #2
0
            public override void ParseResults(XDocument xmlDoc, Exception error)
            {
                TripDetails tripDetail = new TripDetails();

                if (xmlDoc == null || error != null)
                {
                    callback(tripDetail, error);
                }
                else
                {
                    try
                    {

                        tripDetail =
                            (from trip in xmlDoc.Descendants("entry")
                             select ParseTripDetails(trip)).First();
                    }
                    catch (WebserviceResponseException ex)
                    {
                        error = ex;
                    }
                    catch (Exception ex)
                    {
                        error = new WebserviceParsingException(requestUrl, xmlDoc.ToString(), ex);
                    }
                }

                Debug.Assert(error == null);

                callback(tripDetail, error);
            }