public static void Main(string[] args) { var path = @"Data\dane.csv"; var universities = new List <University>(); var studies = new List <Studies>(); var listOfStudents = new HashSet <Student>(new CustomComparer()); //Read from the file var fi = new FileInfo(path); using (var stream = new StreamReader(fi.OpenRead())) { string line = null; var compsci = 0; var newmedia = 0; while ((line = stream.ReadLine()) != null) { string[] columns = line.Split(','); var fname = columns[0]; var lname = columns[1]; var subject = columns[2]; var studytime = columns[3]; var studentnum = int.Parse(columns[4]); var date1 = DateTime.Parse(columns[5]); var email = columns[6]; var mothername = columns[7]; var fathername = columns[8]; Console.WriteLine(line); var studysub = new Studies { name = subject, mode = studytime }; foreach (string x in columns) { if (x.Contains("Informatyka")) { compsci++; } else { newmedia++; } } var studyCount = new StudyCount { compSci = compsci, newMedia = newmedia }; var st = new Student { FirstName = fname, LastName = lname, Email = email, birthdate = date1, StudentIndex = studentnum, mothersName = mothername, fathersName = fathername, studies = studysub }; listOfStudents.Add(st); if (!listOfStudents.Add(st)) { using var sw = new StreamWriter(@"log.txt"); sw.WriteLine($"element with index numver: {st.StudentIndex} already exists"); } var uni = new University { createdAt = "08.03.2020", author = "Patrik Jagielski", students = listOfStudents, activeStudies = studyCount }; universities.Add(uni); } } var str = string.Empty; if (string.IsNullOrEmpty(str)) { } if (string.IsNullOrWhiteSpace(str)) { } //Console.WriteLine($"The number of students in the list is {listOfStudents.Count}"); var writer = new FileStream(@"result.xml", FileMode.Create); var serializer = new XmlSerializer(typeof(List <University>), new XmlRootAttribute("university")); serializer.Serialize(writer, universities); var jsonString = JsonSerializer.Serialize(universities); File.WriteAllText("data.json", jsonString); }
public static HashSet <Student> extractStudents(string csv_path) { var listOfStudents = new HashSet <Student>(new StudentComparer()); var fi = new FileInfo(csv_path); using (var stream = new StreamReader(fi.OpenRead())) { string line = null; while ((line = stream.ReadLine()) != null) { bool doNotAdd = false; string[] columns = line.Split(','); if (columns.Length < 9) { report($"Student with name {columns[0]} {columns[1]} has not enough columns"); continue; } foreach (string column in columns) { if (string.IsNullOrEmpty(column)) { report($"Student with name {columns[0]} {columns[1]} has empty columns"); doNotAdd = true; } } if (doNotAdd) { continue; } var name = columns[0]; var lastName = columns[1]; var index = columns[4]; var birthdate = columns[5]; var email = columns[6]; var mothersName = columns[7]; var fathersName = columns[8]; var studies = new Studies { Name = columns[2], Mode = columns[3] }; var stud = new Student { FirstName = name, LastName = lastName, Email = email, IndexNumber = index, Studies = studies, Birthdate = DateTime.Parse(birthdate), MothersName = mothersName, FathersName = fathersName }; if (!listOfStudents.Add(stud)) { report($"Student {name} {lastName} was not added to the list"); } } } return(listOfStudents); }