I have a problem with my code and a question, i guess i will start with the questions since it might be the easiest, i am using in my code xmlserializer or using namespace System.Xml.Serialization, wondering, which is more of a best practice or is better for memory etc. xml serializer or linq to xml? also anyone know of a good website that teaches linq to xml? linq to sql etc?
my code is suppose to create an xml file using the method xmltoserialize, which it does, the problem is my other method, it does not deserialize the xml file and add them into the listview or called listview1, here is the code below, i double checked it but its still not working :(
please let me know if you have any questions or what am i doing wrong, thanks :)
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
List<UserData> userlist = new List<UserData>();
public void SerializeToXML(List<UserData> info)
{
XmlSerializer serializer = new XmlSerializer(typeof(List<UserData>));
TextWriter textWriter = new StreamWriter(@"C:\userdata.xml");
serializer.Serialize(textWriter, info);
textWriter.Close();
}
public void DeserializeToXml()
{
XmlSerializer deserializer = new XmlSerializer(typeof(List<UserData>));
TextReader textReader = new StreamReader(@"C:\userdata.xml");
List<UserData> userdata = (List<UserData>)deserializer.Deserialize(textReader);
listView1.Items.Clear();
foreach (UserData users in userdata)
{
ListViewItem lvi = new ListViewItem();
lvi.Text = users.firstname;
lvi.SubItems.Add(users.lastname);
lvi.SubItems.Add(users.occupation);
listView1.Items.Add(lvi);
}
textReader.Close();
}
private void btn_submit_Click(object sender, EventArgs e)
{
UserData userinfo = new UserData(txt_firstname.Text, txt_lastname.Text, txt_occupation.Text);
userlist.Add(userinfo);
listView1.Items.Clear();
foreach (UserData listusers in userlist)
{
ListViewItem lvi = new ListViewItem();
lvi.Text = listusers.firstname;
lvi.SubItems.Add(listusers.lastname);
lvi.SubItems.Add(listusers.occupation);
listView1.Items.Add(lvi);
}
txt_firstname.Clear();
txt_lastname.Clear();
txt_occupation.Clear();
btn_save.Click += new EventHandler(delegate
{
SerializeToXML(userlist);
});
btn_upload.Click += new EventHandler(delegate
{
DeserializeToXml();
});
}


Sign In
Create Account


Back to top









