Good afternoon, this task is given:

A file is given, the first three columns contain the last name, first name and patronymic, and the fourth is the email address. Write a program to convert the file to XML

my code is:

public class Person { public string Name { get; set; } public string SName { get; set; } public string OName { get; set; } public string Mail { get; set; } public Person() { } public Person(string name, string sname, string oname, string mail) { Name = name; SName = sname; OName = oname; Mail = mail; } } class Program { static void Main(string[] args) { string filename = @"C:\test\1.txt"; string line; Person person; person = new Person(); // Person[] people = new Person[] { person }; XmlSerializer formatter = new XmlSerializer(typeof(Person)); StreamReader sr = new StreamReader(@filename); while (!sr.EndOfStream) { line = sr.ReadLine(); string[] ss = line.Split(';'); Console.WriteLine(ss[0]); Console.WriteLine(ss[1]); Console.WriteLine(ss[2]); Console.WriteLine(ss[3]); Console.WriteLine("------------"); person.Name = ss[0]; person.SName = ss[1]; person.OName = ss[2]; person.Mail = ss[3]; using (FileStream fs = new FileStream(@"C:\test\people1.xml", FileMode.OpenOrCreate)) { formatter.Serialize(fs, person); } } sr.Close(); 

Since I read data from a file, it turns out that it serializes only the last line, how can I write all the lines?

    2 answers 2

    Do this.

    First, get a class that will contain all instances of Person :

     public class PersonList { public List<Person> Persons { get; set; } } 

    Get an instance in the program:

     var list = new List<Person>(); 

    Read the data in it:

     using (var sr = new StreamReader(filename)) { while (!sr.EndOfStream) { string line = sr.ReadLine(); string[] ss = line.Split(';'); var person = new Person(ss[0], ss[1], ss[2], ss[3]); list.Add(person); } } 

    And write down the whole object:

     var pl = new PersonList() { Persons = list }; var seri = new XmlSerializer(typeof(PersonList)); using (var file = File.Create(@"C:\test\people1.xml")) seri.Serialize(file, pl); 

    Addition: Basically, you can do without even a PersonList :

     var seri = new XmlSerializer(typeof(List<Person>)); using (var file = File.Create(@"C:\test\people1.xml")) seri.Serialize(file, list); 

    (The root element will be ArrayOfPerson .)

    • Thanks for the answer, made using the collection, much easier - Lolidze
    • @Lolidze: Please! - VladD
    • did by your example, much more beautiful and what you need, thanks again - Lolidze
    • @Lolidze: Complete the answer with another option. - VladD pm

    You rewrite the people1.xml file on each iteration of the while loop. You need one stream for the result file throughout the entire read.

     using (FileStream fs = new FileStream(@"C:\test\people1.xml", FileMode.OpenOrCreate)) { while (!sr.EndOfStream) { line = sr.ReadLine(); string[] ss = line.Split(';'); Console.WriteLine(ss[0]); Console.WriteLine(ss[1]); Console.WriteLine(ss[2]); Console.WriteLine(ss[3]); Console.WriteLine("------------"); person.Name = ss[0]; person.SName = ss[1]; person.OName = ss[2]; person.Mail = ss[3]; formatter.Serialize(fs, person); } } 
    • I tried it too, but this way is not displayed correctly, tobish, when opening one naked first line with spaces. ArrayOfPerson needs to be done somehow, for right now it turns out that for every line a Person xmlns is written: xsi = " w3.org/2001/XMLSchema-instance ", and it should be, in theory, all under one - Lolidze