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?

  • <? xml version = "1.0"?> <ArrayOfPerson xmlns: xsi = " w3.org/2001/XMLSchema-instance " xmlns: xsd = " w3.org/2001/XMLSchema "> <Person> <Name> valentin </ Name> <Surname> evseev </ Surname> <PersonnelNumber> 1111 </ PersonnelNumber> </ Person> </ ArrayOfPerson> me> valentin </ Name> <Surname> evseev </ Surname> <PersonnelNumber> 1111 </ PersonnelNumber> < / Person> </ ArrayOfPerson> - Anton Evseev
  • one
    When you close the application, you need to overwrite the file completely. Instead of using OpenOrCreate, use Create when closing. Otherwise, new data is written over the old, and the remnants of the old prevent deserialization. - qzavyer
  • @qzavyer your comment should be the answer - Pavel Mayorov

1 answer 1

When you close the application, you need to overwrite the file completely. Instead of OpenOrCreate , use Create when closing. Otherwise, new data is written over the old, and the remnants of the old prevent deserialization.

  • Yes, now everything is ok! - Anton Evseev