public static void Main(string[] args) { log = ""; string pathin; string pathout; string pathlog = @"log.txt"; if (args.Length > 1) { pathin = args[0]; pathout = args[1]; } else { pathin = @"data.csv"; pathout = @"result.xml"; } try { var students = In(pathin); string date = DateTime.Today.ToShortDateString(); string rattr = $"uczelnia\ncreated at: {date}\nauthor:Jan Przystał"; Uczelnia uczelnia = new Uczelnia(date, "Jan Przystał", students); FileStream writer = new FileStream(pathout, FileMode.Create); XmlSerializer serializer = new XmlSerializer(typeof(Uczelnia)); serializer.Serialize(writer, uczelnia); }catch (ArgumentException e) { writeLog("Podana ścieżka jest niepoprawna"); }catch (FileNotFoundException e) { writeLog("Plik nazwa nie istnieje"); } System.IO.File.WriteAllText(pathlog, log); }
public Uczelnia(Uczelnia uczelnia) { this.activeStudies = uczelnia.activeStudies; this.studenci = uczelnia.studenci; this.author = uczelnia.author; this.createdAt = uczelnia.createdAt; }
public static void Main(string[] args) { log = ""; string pathin, pathout, pathlog = @"log.txt"; if (args.Length <= 1) { pathin = @"data.csv"; pathout = @"result.xml"; } else { pathin = args[0]; pathout = args[1]; } try { var students = In(pathin); string date = DateTime.Today.ToShortDateString(), rattr = $"uczelnia\ncreated at: {date}\nauthor:Rafał Jaglak"; Uczelnia uczelnia = new Uczelnia(date, "Rafał Jaglak", students); FileStream writer = new FileStream(pathout, FileMode.Create); XmlSerializer serializer = new XmlSerializer(typeof(Uczelnia)); serializer.Serialize(writer, uczelnia); } catch (ArgumentException e) { writeLog("Zła ścieżka"); } catch (FileNotFoundException e) { writeLog("Brak pasujących wyników"); } System.IO.File.WriteAllText( pathlog, log ); }
public void write(Uczelnia uczelnia, string outputData) { this.uczelnia = uczelnia; //konfiguracja serializacji var options = new JsonSerializerOptions { WriteIndented = true, }; //serializacja string jsonString = JsonSerializer.Serialize(this, options); /* * usunięcie problemu niepoprawnej strony kodowej - brak polskich znaków */ List <string> plList = new List <string>(); plList.Add("ą"); plList.Add("ć"); plList.Add("ę"); plList.Add("ł"); plList.Add("ń"); plList.Add("ó"); plList.Add("ś"); plList.Add("ź"); plList.Add("ż"); plList.Add("Ą"); plList.Add("Ć"); plList.Add("Ę"); plList.Add("Ł"); plList.Add("Ń"); plList.Add("Ó"); plList.Add("Ś"); plList.Add("Ź"); plList.Add("Ż"); string pl = JsonSerializer.Serialize(plList); var plTab = pl.Substring(1, pl.Length - 1).Split(","); string[] tmp = jsonString.Split(Environment.NewLine); StringBuilder result = new StringBuilder(); string stmp = ""; for (int j = 0; j < tmp.Length; j++) { stmp = tmp[j]; for (int i = 0; i < plTab.Length; i++) { stmp = stmp.Replace(plTab[i].Substring(1, plTab[i].Length - 2), plList.ElementAt(i)); } result.Append(stmp); result.AppendLine(); } //zapis do pliku File.WriteAllText(outputData, result.ToString(), Encoding.UTF8); }
static void Main(string[] args) { // rozpoznanie argumentów i ewentualna korekta wartości domyślnych try { getArgs(args); } catch (ArgumentException ae) { handleException(ae); } try { inputFile = new StreamReader(inputData, System.Text.Encoding.UTF8); } catch (IOException e) { handleException(new FileNotFoundException("Nie znaleziono pliku: " + inputData)); try { inputFile = new StreamReader(inputDataFinal, System.Text.Encoding.UTF8); } catch (FileNotFoundException exc) { string msg = "Nie znaleziono pliku domyślnego: " + inputDataFinal + "\nDZIAŁANIE PROGRAMU PRZERWANE"; Console.WriteLine(msg); logFile.WriteLine(msg); logFile.Close(); return; } } // wczytywanie danych outputTable = ReadFile(); inputFile.Close(); Uczelnia uczelnia = new Uczelnia(new Studenci(outputTable)); // konwersja i zapis do pliku try { writer.write(uczelnia, outputData); } catch (DirectoryNotFoundException e) { handleException(new DirectoryNotFoundException("Katalog nie istnieje: " + outputData)); writer = new MyXmlWriter(); writer.write(uczelnia, outputDataFinal); } catch (UnauthorizedAccessException e) { handleException(new UnauthorizedAccessException("Nie masz dostępu do tego katalogu: " + outputData)); writer = new MyXmlWriter(); writer.write(uczelnia, outputDataFinal); } logFile.Close(); Console.WriteLine("Program wykonał się poprawnie"); }
public static void writeToXML(List <Student> studentsList, string file) { FileStream writer = new FileStream(file, FileMode.Create); XmlSerializer serializer = new XmlSerializer(typeof(Uczelnia)); Uczelnia uczelnia = new Uczelnia { StudentsList = studentsList, Author = "Tomasz Palczewski", CreatedAt = DateTime.Now.ToString() }; serializer.Serialize(writer, uczelnia); }
public static void writeToJson(List <Student> studentsList, string file) { Uczelnia uczelnia = new Uczelnia { StudentsList = studentsList, Author = "Tomasz Palczewski", CreatedAt = DateTime.Now.ToString() }; string jsonString; jsonString = JsonSerializer.Serialize(uczelnia); File.WriteAllText(file, jsonString); }
public void write(Uczelnia uczelnia, string outputData) { var outputFile = new StreamWriter(outputData); var serializer = new XmlSerializer(typeof(Uczelnia), new XmlRootAttribute("uczelnia")); XmlSerializerNamespaces nsSerializer = new XmlSerializerNamespaces(); nsSerializer.Add(string.Empty, string.Empty); //konfiguracja serializacji XmlWriterSettings settings = new XmlWriterSettings(); settings.OmitXmlDeclaration = true; settings.NewLineOnAttributes = false; settings.Indent = true; settings.IndentChars = " "; settings.Encoding = System.Text.Encoding.UTF8; XmlWriter writer = XmlWriter.Create(outputFile, settings); //serializacja serializer.Serialize(writer, uczelnia, nsSerializer); }
static void Main(string[] args) { string CSVPath; string DestPath; string Format; try { CSVPath = args[0]; DestPath = args[1]; Format = args[2]; }catch (Exception) { CSVPath = @"D:\Users\User\Desktop\Cw2\Cw2\dane.csv"; DestPath = @"D:\Users\User\Desktop\Cw2\Cw2\result.xml"; Format = "XML"; } var lines = File.ReadLines(CSVPath); HashSet <Student> set = new HashSet <Student>(new Comparator()); List <Course> studiesList = new List <Course>(); foreach (var line in lines) { var tmp = line.Split(","); Course course = new Course { name = tmp[2], numberOfStudents = 0 }; var tmpStudent = new Student { fname = tmp[0], lname = tmp[1], indexNumber = "s" + tmp[4], studies = new StCourse { name = tmp[2], mode = tmp[3] }, birthdate = tmp[5].Length >= 9 ? tmp[5].Substring(0, 9) : "", email = tmp[6], mothersName = tmp[7], fathersName = tmp[8] }; if (set.Add(tmpStudent)) { if (!studiesList.Contains(course)) { studiesList.Add(course); } studiesList[studiesList.IndexOf(course)].numberOfStudents++; } } Uczelnia raport = new Uczelnia { author = "Artur Ostapuk", createdAt = DateTime.Today.ToShortDateString(), studenci = set, activeStudies = studiesList }; switch (Format) { case "XML": FileStream writer = new FileStream(DestPath, FileMode.Create); XmlSerializer serializer = new XmlSerializer(typeof(Uczelnia)); serializer.Serialize(writer, raport); break; case "JSON": JsonSerializerOptions tmpOp = new JsonSerializerOptions(); tmpOp.WriteIndented = true; var tmpWrapper = new { uczelnia = raport }; File.WriteAllText(DestPath, JsonSerializer.Serialize(tmpWrapper, tmpOp)); break; } }
static void Main(string[] args) { var log = new Log(); try { // var pathCsv = @"/Users/admin/Desktop/Schools4/APBD/projects/Cw2/Cw2/Cw2/Data/dane.csv"; var pathCsv = args.Length > 0 ? args[0] : "data.csv"; var destination = args.Length > 1 ? args[1] : "result.xml"; var fileFormat = args.Length > 2 ? args[2] : "xml"; var lines = File.ReadLines(pathCsv); var uczelnia = new Uczelnia(); var students = new HashSet <Student>(new MyComparer()); var activeStudiesDictionary = new Dictionary <string, int>(); foreach (var line in lines) { try { var data = line.Split(","); if (data.Length == 9 && !Array.Exists(data, element => element == "")) { Student tmp = new Student(data); if (students.Add(tmp)) { if (!activeStudiesDictionary.ContainsKey(tmp.studies.name)) { activeStudiesDictionary.Add(tmp.studies.name, 1); } else { activeStudiesDictionary[tmp.studies.name] += 1; } } } else { throw new StudentException(line); } } catch (StudentException e) { log.WriteLine(e.Msg + " nie został dodany"); } } Console.WriteLine(students.Count); uczelnia.studenci = students; uczelnia.activeStudies = new List <ActiveStudies>(); foreach (KeyValuePair <string, int> kvp in activeStudiesDictionary) { uczelnia.activeStudies.Add(new ActiveStudies { name = kvp.Key, numberOfStudents = kvp.Value }); } if (fileFormat.Equals("xml")) { FileStream writer = new FileStream(destination, FileMode.Create); XmlSerializer serializer = new XmlSerializer(typeof(Uczelnia)); serializer.Serialize(writer, uczelnia); } else if (fileFormat.Equals("json")) { var jsonString = JsonSerializer.Serialize(uczelnia); File.WriteAllText("data.json", jsonString); } } catch (ArgumentException e) { log.WriteLine($"Podana sciezka jest niepoprawna: {e.ParamName}"); } catch (FileNotFoundException e) { log.WriteLine($"Plik {e.FileName} nie istnieje"); } }
public static void Main(string[] args) { string pathToCsv; string pathToResult; string conversionType; if (args.Length == 3) { pathToCsv = args[0]; pathToResult = args[1]; conversionType = args[2]; } else if (args.Length == 1) { pathToCsv = args[0]; pathToResult = @"result.xml"; conversionType = "xml"; } else if (args.Length == 2) { pathToCsv = args[0]; pathToResult = args[1]; conversionType = "xml"; } else if (args.Length == 0) { pathToCsv = @"dane.csv"; pathToResult = @"result.xml"; conversionType = "xml"; } else { Console.Error.Write("Niepoprawna liczba argumentow!"); return; } HashSet <Student> studenci = new HashSet <Student>(new OwnComparer()); Dictionary <string, Studies> studiesDict = new Dictionary <string, Studies>(); StringBuilder logsb = new StringBuilder(); logsb.Append("Studenci nie dodani z powodu blednych danych:"); logsb.AppendLine(); try { var lines = File.ReadLines(pathToCsv); bool ok; foreach (var line in lines) { ok = true; string[] student = line.Split(','); if (student.Length != 9) { logsb.Append(line + "\t | zla ilosc danych!"); logsb.AppendLine(); continue; } foreach (var pole in student) { if (string.IsNullOrEmpty(pole) || string.IsNullOrWhiteSpace(pole)) { ok = false; logsb.Append(line + "\t | puste pole!"); logsb.AppendLine(); break; } } if (!ok) { continue; } Studies studiestmp = new Studies { name = student[2], mode = student[3] }; Student st = new Student { fname = student[0], lname = student[1], studies = studiestmp, indexNumber = student[4], birthdate = student[5], email = student[6], mothersName = student[7], fathersName = student[8] }; if (!studenci.Add(st)) { logsb.Append(line + "\t | powtorka studenta!"); logsb.AppendLine(); ok = false; } if (ok) { if (studiesDict.ContainsKey(studiestmp.name)) { studiesDict[studiestmp.name].numberOfStudents++; } else { studiesDict[studiestmp.name] = studiestmp; } } } } catch (ArgumentException e) { logsb.AppendLine(); logsb.Append("=================="); logsb.AppendLine(); logsb.Append(e); Console.WriteLine("Podana sciezka jest niepoprawna!"); } catch (FileNotFoundException e) { logsb.AppendLine(); logsb.Append("=================="); logsb.AppendLine(); logsb.Append(e); Console.WriteLine("Plik " + pathToCsv + " nie istnieje!"); } File.WriteAllText(@"log.txt", logsb.ToString()); var today = DateTime.Today; List <ActiveStudies> activeStudies = new List <ActiveStudies>(); foreach (var keyPar in studiesDict) { ActiveStudies tmp = new ActiveStudies() { name = keyPar.Value.name, numberOfStudents = keyPar.Value.numberOfStudents }; activeStudies.Add(tmp); } Uczelnia uczelnia = new Uczelnia() { author = "Jakub Oleksiak", createdAt = today.ToShortDateString(), studenci = studenci, activeStudies = activeStudies }; if (conversionType.Equals("xml")) { FileStream toResWriter = new FileStream(pathToResult, FileMode.Create); XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add("", ""); XmlSerializer xmlSerializer = new XmlSerializer(typeof(Uczelnia)); xmlSerializer.Serialize(toResWriter, uczelnia, ns); toResWriter.Close(); toResWriter.Dispose(); } else if (conversionType.Equals("json")) { JsonSerializerOptions jsonSerializerOptions = new JsonSerializerOptions(); jsonSerializerOptions.WriteIndented = true; Package package = new Package(); package.uczelnia = new Uczelnia(uczelnia); var json = JsonSerializer.Serialize(package, jsonSerializerOptions); File.WriteAllText(pathToResult, json); } }
static void Main(string[] args) { try { var daneFi = new FileInfo(@"dane.csv"); var xmlFi = new FileInfo(@"data.xml"); if (args.Length > 0 && args.Length != 3) { throw new ArgumentOutOfRangeException(); } if (args.Length == 3) { daneFi = new FileInfo(args[0]); if (!daneFi.Exists) { throw new FileNotFoundException("Plik z danymi nie istnieje"); } xmlFi = new FileInfo(args[1]); if (xmlFi.Directory != null && !xmlFi.Directory.Exists) { throw new ArgumentException("Podana ścieżka jest niepoprawna"); } if (!args[2].Equals("xml")) { throw new ArgumentException("Niewspierany format pliku wyjściowego"); } } var studenciDic = new Dictionary <string, Student>(); var activeStudiesDic = new Dictionary <string, ActiveStudy>(); var logFi = new FileInfo(@"log.txt"); using (var logWriter = new StreamWriter(logFi.OpenWrite())) using (var daneReader = new StreamReader(daneFi.OpenRead())) { string line; while ((line = daneReader.ReadLine()) != null) { try { string[] studentInfo = line.Split(","); if (studentInfo.Length != 9) { throw new Exception(message: DateTime.Now.ToString(CultureInfo.CurrentCulture) + " Niepoprawna ilość danych w wierszu: " + line); } if (studentInfo.Any(info => info.Trim().Length == 0)) { throw new Exception(message: DateTime.Now.ToString(CultureInfo.CurrentCulture) + " Puste dane w wierszu: " + line); } Student student = new Student { IndexNumber = "s" + studentInfo[4], FName = studentInfo[0], LName = studentInfo[1], Birthdate = DateTime.Parse(studentInfo[5]).ToShortDateString(), Email = studentInfo[6], MothersName = studentInfo[7], FathersName = studentInfo[8], StudiesNameAndMode = new Studies { Name = studentInfo[2], Mode = studentInfo[3] } }; string key = student.FName + " " + student.LName + " s" + student.IndexNumber; if (studenciDic.ContainsKey(key)) { throw new Exception(DateTime.Now.ToString(CultureInfo.CurrentCulture) + " Student już istnieje: " + line); } studenciDic.Add(key, student); key = student.StudiesNameAndMode.Name; if (activeStudiesDic.ContainsKey(key)) { activeStudiesDic[key].NumberOfStudents++; } else { activeStudiesDic.Add(key, new ActiveStudy { Name = key, NumberOfStudents = 1 }); } } catch (Exception e) { logWriter.WriteLine(e.Message); } } } var uczelnia = new Uczelnia { CreatedAt = DateTime.Now.ToShortDateString(), Author = "Mateusz Zaborowski", Studenci = studenciDic.Values.ToArray(), ActiveStudies = activeStudiesDic.Values.ToArray() }; var xmlWriter = new StreamWriter(xmlFi.OpenWrite()); var xns = new XmlSerializerNamespaces(); xns.Add(string.Empty, string.Empty); var xmlSerializer = new XmlSerializer(typeof(Uczelnia)); xmlSerializer.Serialize(xmlWriter, uczelnia, xns); } catch (ArgumentOutOfRangeException e) { Console.WriteLine(e.GetType() + ": " + e.Message); } catch (FileNotFoundException e) { Console.WriteLine(e.GetType() + " " + e.Message); } catch (ArgumentException e) { Console.WriteLine(e.GetType() + ": " + e.Message); } }
static void Main(string[] args) { string startupPath = Directory.GetParent(Environment.CurrentDirectory).Parent.Parent.FullName; string csvFile = args.Length > 0 && args[0].EndsWith("csv") ? args[0] : startupPath + @"\data.csv"; string xmlFile = args.Length > 1 && args[1].EndsWith("xml") ? args[1] : startupPath + @"\żesult.xml"; string type = args.Length > 2 && args[2].EndsWith("xml") ? args[2] : "xml"; string logFile = startupPath + @"łog.txt"; FileInfo fileInfoCsv = new FileInfo(csvFile); FileInfo fileInfoLog = new FileInfo(logFile); var list = new List <Student>(); try { StreamWriter logStream = new StreamWriter(fileInfoLog.OpenWrite()); try { StreamReader stream = new StreamReader(fileInfoCsv.OpenRead()); string line = ""; while ((line = stream.ReadLine()) != null) { string[] words = line.Split(','); bool tmp = true; words[1] = Regex.Replace(words[1], "[0-9]", ""); foreach (Student student in list) { if ((words[0].Equals(student.Imie) && words[1].Equals(student.Nazwisko) && ("s" + words[4]).Equals(student.Index)) || words.Length != 9) { tmp = false; } } for (int i = 0; i < words.Length; i++) { if (words[i].Equals(" ")) { tmp = false; } } words[2] = words[2].Replace("dzienne", ""); words[2] = words[2].Replace("zaoczne", ""); if (tmp) { list.Add(new Student { Imie = words[0], Nazwisko = words[1], Studies = new Studies(words[2], words[3]), Index = "s" + words[4], BirthDate = words[5], Email = words[6], MothersName = words[7], FathersName = words[8] }); } if (!tmp) { logStream.Write("Problem with:" + line + "\n"); } } stream.Close(); } catch (ArgumentException ex) { Console.WriteLine("Podana ścieżka jest niepoprawna"); logStream.Write(ex.Message + "\n"); } catch (FileNotFoundException ex) { Console.WriteLine("Plik " + csvFile + " nie istnieje"); logStream.Write(ex.Message + "\n"); } logStream.Close(); } catch (IOException ex) { Console.WriteLine(ex.Message); } if (type == "xml") { using (var sw = new FileStream(xmlFile, FileMode.Create)) { var uczelnia = new Uczelnia() { Author = "Magdalena Tomczak-Kolodzicka", Studenci = list }; new XmlSerializer(typeof(Uczelnia)).Serialize(sw, uczelnia); } } }