Cześć, udało mi się stworzyć serializację danych do pliku, natomiast nie mogę sobie poradzić z odczytem.
public class Person
{
[XmlAttribute("Id")]
public int PersonId { get; set; }
[XmlElement("Imie")]
public string FirstName { get; set; }
[XmlElement("Nazwisko")]
public string LastName { get; set; }
[XmlElement("Wiek")]
public int Age { get; set; }
public Person()
{
}
public Person(int nPersonId, string sFirstName, string sLastName, int nAge)
{
PersonId = nPersonId;
FirstName = sFirstName;
LastName = sLastName;
Age = nAge;
}
}
class Program
{
static void Main(string[] args)
{
List<Person> oPersonsList = new List<Person>();
int nCounter = 0;
oPersonsList.Add(new Person(++nCounter, "Jan", "Kowalski", 23));
oPersonsList.Add(new Person(++nCounter, "Agnieszka", "Nowak", 22));
XmlRootAttribute oRootAttr = new XmlRootAttribute();
oRootAttr.ElementName = "Persons";
oRootAttr.IsNullable = true;
XmlSerializer oSerializer = new XmlSerializer(typeof(List<Person>), oRootAttr);
StreamWriter oStreamWriter = null;
try
{
oStreamWriter = new StreamWriter("person.xml");
oSerializer.Serialize(oStreamWriter, oPersonsList);
}
catch (Exception oException)
{
Console.WriteLine("Aplikacja wygenerowała następujący wyjątek: " + oException.Message);
}
finally
{
if (null != oStreamWriter)
{
oStreamWriter.Dispose();
}
}
StreamReader reader = new StreamReader("person.xml");
XmlSerializer deserializer = new XmlSerializer(typeof(List<Person>));
Console.WriteLine("Dane osoby:");
Console.WriteLine("Imie: ");
Console.WriteLine("Nazwisko: ");
Console.WriteLine("Wiek: ");
Console.ReadLine();
}
}
Jakieś podpowiedzi ^_^?