Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do i make a ReadFromXML method

i cant figure out how to get the ReadFromXML Method to work

thats my code and i tried what i know but i just cant get it to work

    class XMLOperators
    {
        public static void Write2XML(string path, List<Person> people)
        {
            var writer = new XmlSerializer(typeof(List<Person>));
            var file = File.Create(path);
            writer.Serialize(file, people);
            file.Close();
        }

        public static void ChangeXMLData(string path)
        {
            var dataFromXML = ReadFromXML(path);
            foreach (var person in dataFromXML)
            {
                Console.WriteLine(person.Name);
                Console.WriteLine(person.Birthday);
            }
            Console.WriteLine("Welche Person wollen Sie bearbeiten, "+
                "geben Sie den Namen ein:");
            var name = Console.ReadLine();
            for (int i = 0; i < dataFromXML.Count; i++)
            {
                if (dataFromXML[i].Name.ToLower()==name.ToLower())
                {
                    Console.WriteLine("Was wollen Sie ändern? Geburtsdatum oder Name?");
                    var desicion = Console.ReadLine();
                    if (desicion.ToLower() == "geburtsdatum")
                    {
                        Console.WriteLine("Geburtsdatum ist: " + dataFromXML[i].Birthday);
                        Console.WriteLine("Bitte geben Sie das neue Geburtsdatum ein:");
                        Console.WriteLine("Jahr:");
                        var year = Convert.ToInt32(Console.ReadLine());
                        Console.WriteLine("Monat:");
                        var month = Convert.ToInt32(Console.ReadLine());
                        Console.WriteLine("Tag:");
                        var day = Convert.ToInt32(Console.ReadLine());

                        dataFromXML[i].Birthday = new DateTime(year, month, day);
                    }
                    else if (desicion.ToLower() == "name")
                    {
                        Console.WriteLine("Bitte geben Sie den neuen Namen ein");
                        var newName = Console.ReadLine();
                        dataFromXML[i].Name = newName;
                    }
                    else { Console.WriteLine("Die Eingabe war falsch"); }
                }
            }
            Write2XML(path, dataFromXML);
        }
    }

i tried to do it like i did it for the Write2XML, but i think i’m missing something

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

i think there is the "deserialize" missing but i dont know how to do it

>Solution :

i think what you are missing is something like this

public static List<Person> ReadFromXML(string path)
    {
        var reader = new XmlSerializer(typeof(List<Person>));
        var file = new StreamReader(path);
        var people=(List<Person>)reader.Deserialize(file);
        file.Close();
        return people;
    }

so yea you seserialize with
var people=(List)reader.Deserialize(file);
and also return people;
the rest is the same like from your Write2XML
hope that works for you

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading