Exemplo n.º 1
0
        public ICollection<Flight> ImportFlightsDataFromFile(string filePath)
        {
            if (!Regex.IsMatch(filePath, FlightsXmlFilePattern))
            {
                throw new ArgumentException(string.Format(InvalidFileNameMessage, "Flights"));
            }

            ICollection<Flight> importedFlights = new HashSet<Flight>();

            using (XmlReader reader = XmlReader.Create(filePath))
            {
                while (reader.Read())
                {
                    try
                    {
                        if (reader.NodeType == XmlNodeType.Element && reader.Name == "flight")
                        {
                            int flightId = int.Parse(reader.GetAttribute("id"));
                            string flightCode = reader.GetAttribute("code").ToString();
                            DateTime flightDate = DateTime.Parse(reader.GetAttribute("dateTime"));

                            reader.ReadToDescendant("airline");
                            int airlineId = int.Parse(reader.GetAttribute("airlineId"));

                            reader.ReadToNextSibling("departureAirport");
                            int departureAirportId = int.Parse(reader.GetAttribute("departureAirportId"));

                            reader.ReadToNextSibling("arrivalAirport");
                            int arrivalAirportId = int.Parse(reader.GetAttribute("arrivalAirportId"));

                            reader.ReadToNextSibling("duration");
                            double durationHours = reader.ReadElementContentAsDouble();

                            var flight = new Flight()
                            {
                                FlightId = flightId,
                                FlightCode = flightCode,
                                AirlineId = airlineId,
                                FlightDate = flightDate,
                                DurationHours = durationHours,
                                DepartureAirportId = departureAirportId,
                                ArrivalAirportId = arrivalAirportId
                            };

                            importedFlights.Add(flight);
                        }
                    }
                    catch (FormatException)
                    { }
                }
            }

            return importedFlights;
        }
Exemplo n.º 2
0
        public ICollection<Flight> ImportFlightsDataFromFile(string filePath)
        {
            if (!Regex.IsMatch(filePath, FlightsWorksheetFilePattern))
            {
                throw new ArgumentException(string.Format(InvalidFileNameMessage, "Flights"));
            }

            OleDbConnection connection = new OleDbConnection();
            connection.ConnectionString = string.Format(AirportsDbSettings.Default.ExcelReaderConnectionString, filePath);

            connection.Open();

            using (connection)
            {
                var schema = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                var sheetName = schema.Rows[0]["TABLE_NAME"].ToString();

                OleDbCommand selectAllRowsCommand = new OleDbCommand("SELECT * FROM [" + sheetName + "]", connection);

                ICollection<Flight> importedFlights = new HashSet<Flight>();

                using (OleDbDataAdapter adapter = new OleDbDataAdapter(selectAllRowsCommand))
                {
                    DataSet dataSet = new DataSet();
                    adapter.Fill(dataSet);

                    using (DataTableReader reader = dataSet.CreateDataReader())
                    {
                        while (reader.Read())
                        {
                            try
                            {
                                string flightCode = reader["FlightCode"].ToString();
                                int airlineId = int.Parse(reader["AirlineId"].ToString());
                                int departureAirportId = int.Parse(reader["DepartureAirportId"].ToString());
                                int arrivalAirportId = int.Parse(reader["ArrivalAirportId"].ToString());
                                double durationHours = double.Parse(reader["Duration"].ToString());
                                DateTime date = DateTime.Parse(reader["DateTime"].ToString(), CultureInfo.InvariantCulture);

                                var flight = new Flight()
                                {
                                    FlightCode = flightCode,
                                    AirlineId = airlineId,
                                    FlightDate = date,
                                    DurationHours = durationHours,
                                    DepartureAirportId = departureAirportId,
                                    ArrivalAirportId = arrivalAirportId
                                };

                                importedFlights.Add(flight);
                            }
                            catch (FormatException)
                            { }
                        }
                    }
                }

                return importedFlights;
            }
        }