Example #1
0
        public void AddInXml(Person person,string path)
        {
            if (!File.Exists(path))
                throw new FileNotFoundException();

            XmlDocument doc = new XmlDocument();
            doc.Load(path);
            XmlElement newPerson = doc.CreateElement("Person");

            XmlElement newName = doc.CreateElement("Name");
            newName.InnerText = person.Name;
            newPerson.AppendChild(newName);

            XmlElement newAge = doc.CreateElement("Age");
            newAge.InnerText = person.Age.ToString();
            newPerson.AppendChild(newAge);

            XmlElement newSex = doc.CreateElement("Sex");
            newSex.InnerText = person.Sex.ToString();
            newPerson.AppendChild(newSex);

            XmlElement newRace = doc.CreateElement("Race");
            newRace.InnerText = person.Race.ToString();
            newPerson.AppendChild(newRace);

            XmlElement newFavoriteCar = doc.CreateElement("FavoriteCar");
            newFavoriteCar.InnerText = person.FavoriteCar;
            newPerson.AppendChild(newFavoriteCar);

            doc.DocumentElement.AppendChild(newPerson);
            doc.Save(path);
        }
Example #2
0
        public List<Person> GetPersons(string path, Encoding enc)
        {
            String []text = File.ReadAllLines(path,enc);
            List<Person> persons = new List<Person>();

            foreach (var s in text)
            {
                string []parametrs = s.Split(',');
                var person = new Person();
                person.Name = parametrs[0];
                person.Age = Int32.Parse(parametrs[1]);
                person.Sex = parametrs[2].ToEnum<Sex>();
                person.Race = parametrs[3].ToEnum<Race>();
                person.FavoriteCar = parametrs[4];

                persons.Add(person);
            }

            return persons;
        }