public static void SaveFileToXML(AviaTicket ticket) { DirectoryInfo dir = new DirectoryInfo(@".\XmlFiles"); if (!dir.Exists) { dir.Create(); } var date = DateTime.Now; string path = string.Format(@"{0}\{1}.{2}.{3} {4}.{5}.{6}.xml", dir.FullName, date.Day, date.Month, date.Year, date.Hour, date.Minute, date.Millisecond); using (FileStream stream = new FileStream(path, FileMode.CreateNew)) { XmlSerializer xmlSerializer = new XmlSerializer(typeof(AviaTicket)); xmlSerializer.Serialize(stream, ticket); } }
// ====================== Avia Ticket Parser ====================== public AviaTicket ParseAviaTicket(string path) { AviaTicket ticket = new AviaTicket(); try { string content = Extractor.Extract(path); Lines = content.Split('\r', '\n'); #region Avia Ticket ticket.Date = GetValue("DATE"); ticket.Agent = GetValue("AGENT"); ticket.Name = GetValue("NAME"); ticket.Fqtv = GetValue("FQTV"); ticket.Iata = GetValue("IATA"); ticket.Telephone = GetValue("TELEPHONE"); ticket.IssuingAirline = GetValue("ISSUING AIRLINE"); ticket.TicketNumber = GetValue("TICKET NUMBER"); ticket.Endorsements = GetValue("ENDORSEMENTS"); ticket.ExchangeRate = GetValue("EXCHANGE RATE"); ticket.Payment = GetValue("PAYMENT"); ticket.FareCalculation = GetValueFareCalculation("FARE CALCULATION"); ticket.AirFare = GetValue("AIR FARE"); ticket.EquivFarePaid = GetValue("EQUIV FARE PAID"); ticket.AirlineSurcharges = GetValue("AIRLINE SURCHARGES"); ticket.Total = GetValue("TOTAL"); ticket.Tax = GetValueTax("TAX"); ticket.FlightInfos = GetFlightsInfo() as FlightInfo[]; #endregion return(ticket); } catch (Exception ex) { Console.WriteLine("An error occurred while trying to convert."); Console.WriteLine(path); } return(ticket); }
public static void MainLogic(object sender) { try { // Получаем все письма с почтового ящика //IEnumerable<MailMessage> messages = GetMessages(); // Сохраняем полученные файлы во временный каталог //SaveMail(messages); // Получаем все файлы с информацией об авиа билетах и багаже IEnumerable <FileInfo> aviaFiles = GetFiles("electronic ticket"); IEnumerable <FileInfo> baggageFiles = GetFiles("baggage"); Parser parser = new Parser(); AviaTicket ticket = null; AviaBaggage baggage = null; foreach (FileInfo item in aviaFiles) { try { ticket = parser.ParseAviaTicket(item.FullName); } catch (Exception ex) { Console.WriteLine(item.FullName); } using (MailDb db = new MailDb()) { var query = from t in db.Tickets where t.TicketNumber == ticket.TicketNumber select t; if (query.Count() == 0) { db.Tickets.Add(new Ticket() { TicketNumber = ticket.TicketNumber, Agent = ticket.Agent }); db.SaveChanges(); SaveFileToXML(ticket); } } } foreach (FileInfo item in baggageFiles) { try { baggage = parser.ParseAviaBaggage(item.FullName); } catch (Exception ex) { Console.WriteLine(item.FullName); } using (MailDb db = new MailDb()) { var query = from b in db.Baggages where b.BaggageNumber == baggage.No select b; if (query.Count() == 0) { db.Baggages.Add(new Baggage() { BaggageNumber = baggage.No }); db.SaveChanges(); SaveFileToXML(baggage); } } } Console.WriteLine("End parsing!"); DirectoryInfo tempFolder = new DirectoryInfo(@".\AviaTicketsFromMail"); try { //tempFolder.Delete(true); } catch (Exception ex) { } } catch (Exception ex) { string str = $"Date: {DateTime.Now}\nError: {ex.Message}\nStackTrace: {ex.StackTrace}"; using (StreamWriter writer = new StreamWriter(@"error.log", true, Encoding.UTF8)) { writer.WriteLine(str); } } }