public static void SaveFileToXML(AviaBaggage baggage) { 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}_Baggage.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(AviaBaggage)); xmlSerializer.Serialize(stream, baggage); } }
// ====================== Avia Baggage Parser ====================== public AviaBaggage ParseAviaBaggage(string path) { AviaBaggage baggage = new AviaBaggage(); try { string content = Extractor.Extract(path); Lines = content.Split('\r', '\n'); for (int i = 0; i < Lines.Length; i++) { if (Lines[i].Contains("**********************")) { StartLine = i; break; } } #region Forming AviaBaggage baggage.No = GetNumber(); baggage.Airport = GetAirport(); baggage.Date = GetFlightDate(); baggage.Company = GetWorldServiceGroup(); baggage.Initials = GetSurnameName(); baggage.NumberAN = GetNumbersAfterName(); baggage.CPN = GetFlightInfos(); #endregion return(baggage); } catch (Exception ex) { Console.WriteLine(ex.Message); } return(baggage); }
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); } } }