using System; using System.Collections.Generic; using System.Collections; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; using System.Xml.Serialization; using System.Collections.Specialized; using System.Runtime.Serialization.Formatters.Binary; namespace Database { public class Program { static void Main(string[] args) { string line = ""; using (StreamReader sr = new StreamReader("option.ini.txt", System.Text.Encoding.Default)) { line = sr.ReadLine(); } List<Person> employees = new List<Person>(); XmlSerializer xs = new XmlSerializer(typeof(List<Person>)); BinaryFormatter bf = new BinaryFormatter(); if (line == "xml") { if (File.Exists("person.xml")) { using (FileStream fs = new FileStream("person.xml", FileMode.OpenOrCreate)) { List<Person> newEmployees = (List<Person>)xs.Deserialize(fs); Console.WriteLine("ΠΠ±ΡΠ΅ΠΊΡ Π΄Π΅ΡΠ΅ΡΠΈΠ°Π»ΠΈΠ·ΠΎΠ²Π°Π½"); employees = newEmployees; } } } if (line == "bin") { if (File.Exists("person.dat")) { using (FileStream fs = new FileStream("person.dat", FileMode.OpenOrCreate)) { List<Person> newEmployees = (List<Person>)bf.Deserialize(fs); employees = newEmployees; } } } if (line == "bin"||line == "xml") { Console.WriteLine("Π£ΡΡΠ°Π½ΠΎΠ²ΠΈΡΠ΅ Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅ bin ΠΈΠ»ΠΈ xml Π² ΡΠ°ΠΉΠ»Π΅ option.ini.txt"); } while (true) { Console.Clear(); Console.WriteLine("ΠΠ²Π΅Π΄ΠΈΡΠ΅ ΠΊΠΎΠΌΠ°Π½Π΄Ρ:"); string com = Console.ReadLine(); if (com.ToLower() == "exit") { if (line == "xml") { using (FileStream fs = new FileStream("person.xml", FileMode.OpenOrCreate)) { xs.Serialize(fs, employees); } return; } else { using (FileStream fs = new FileStream("person.dat", FileMode.OpenOrCreate)) { bf.Serialize(fs, employees); } return; } } if (com.ToLower() == "allsee") { if (line == "xml") { Console.WriteLine("ΠΠΌΡ: {0}, Π€Π°ΠΌΠΈΠ»ΠΈΡ: {1}, Π’Π°Π±Π΅Π»ΡΠ½ΡΠΉ Π½ΠΎΠΌΠ΅Ρ {2}", string.Join(Environment.NewLine, employees.Select(p => p.Name)), string.Join(Environment.NewLine, employees.Select(p => p.Surname)), string.Join(Environment.NewLine, employees.Select(p => p.PersonnelNumber))); Console.ReadLine(); continue; } if (line == "bin") { Console.WriteLine("ΠΠΌΡ: {0}", string.Join(Environment.NewLine, employees.Select(p => p.Name))); Console.ReadLine(); continue; } } if (com.ToLower() == "del") { string n; Console.WriteLine("Name del?"); n = Console.ReadLine(); if (employees.Exists(p => p.Name == n)) { for (int i = 0; i < employees.Count; i++) { if (employees[i].Name == n) { employees.Remove(employees[i]); } } Console.WriteLine("Π£Π΄Π°Π»Π΅Π½ΠΈΠ΅ ΠΏΡΠΎΡΠ»ΠΎ ΡΡΠΏΠ΅ΡΠ½ΠΎ."); } else { Console.WriteLine("Π‘ΠΎΡΡΡΠ΄Π½ΠΈΠΊΠ° Ρ ΡΠ°ΠΊΠΈΠΌ ΠΈΠΌΠ΅Π½Π΅ΠΌ Π½Π΅ ΡΡΡΠ΅ΡΡΠ²ΡΠ΅Ρ!"); } Console.ReadKey(); continue; } if (com.ToLower() == "add") { string name; string surname; string personnelnumber; Console.WriteLine("ΠΠ²Π΅Π΄ΠΈΡΠ΅ ΠΈΠΌΡ ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠ°:"); name = Console.ReadLine(); Console.WriteLine("ΠΠ²Π΅Π΄ΠΈΡΠ΅ ΡΠ°ΠΌΠΈΠ»ΠΈΡ ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠ°:"); surname = Console.ReadLine(); Console.WriteLine("ΠΠ²Π΅Π΄ΠΈΡΠ΅ ΡΠ°Π±Π΅Π»ΡΠ½ΡΠΉ Π½ΠΎΠΌΠ΅Ρ ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠ°:"); personnelnumber = Console.ReadLine(); Person p = new Person(name, surname, personnelnumber); employees.Add(p); continue; } if (com.ToLower() != "add") { Console.WriteLine("ΠΠΎΡΡΡΠΏΠ½ΡΠ΅ ΠΊΠΎΠΌΠ°Π½Π΄Ρ:"); Console.WriteLine("\tadd ΡΠΎΠ·Π΄Π°ΡΡ Π·Π°ΠΏΠΈΡΡ ΡΠΎΡΡΡΠ΄Π½ΠΈΠΊΠ°"); Console.WriteLine("\texit ΠΡΠΉΡΠΈ ΠΈ ΡΠΎΡ
ΡΠ°Π½ΠΈΡΡ Π΄Π°Π½Π½ΡΠ΅"); Console.WriteLine("\tallsee ΠΡΠΉΡΠΈ ΠΈ ΡΠΎΡ
ΡΠ°Π½ΠΈΡΡ Π΄Π°Π½Π½ΡΠ΅"); Console.ReadKey(); continue; } } } } [Serializable] public class Person { public string Name { get; set; } public string Surname { get; set; } public string PersonnelNumber { get; set; } public Person() { } public Person(string name, string surname, string personnelnumber) { Name = name; Surname = surname; PersonnelNumber = personnelnumber; } } }
Hello! When adding employees, the addition is successful. When the program is closed, the serialization to the person.xml file is also successful. The next time you open, it reads. But when deleting an employee, the deletion is also successful, but when exiting, the serialization is already uncorrelated. valentin ivanov 1111 me> valentin ivanov 1111 As a result, when calling any exeption command. How to fix?