My application has a ListView control that has data added to it across several columns and rows. When the form is closed (calling the Form_Closing event), the contents are saved to an XML file. Then, at the following run time, the XML document is read and its contents are displayed in the ListView control. For some reason, it only loads the first column and doesn't save all of the data. I am new to using XML to save data. Any and all help will be appreciated.
This is what I have so far, I'm sure there are plenty of errors:
private void Form1_Load(object sender, EventArgs e)
{
System.Xml.XmlDocument loadDoc = new System.Xml.XmlDocument();
loadDoc.Load(Application.StartupPath + "\\Accounts.xml");
foreach (System.Xml.XmlNode emailNode in loadDoc.SelectNodes("/Accounts/Item"))
{
AccountList.Items.Add(emailNode.Attributes["email"].InnerText);;
}
foreach (System.Xml.XmlNode passwordNode in loadDoc.SelectNodes("/Accounts/Item"))
{
AccountList.Items.Add(passwordNode.Attributes["password"].InnerText); ;
}
foreach (System.Xml.XmlNode statusNode in loadDoc.SelectNodes("/Accounts/Item"))
{
AccountList.Items.Add(statusNode.Attributes["status"].InnerText); ;
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
System.Xml.XmlTextWriter writer = new System.Xml.XmlTextWriter(Application.StartupPath + "\\Accounts.xml", null);
writer.WriteStartElement("Accounts");
for (int i =0; i < AccountList.Items.Count; i++)
{
writer.WriteStartElement("Item");
writer.WriteAttributeString("email", AccountList.Items[i].Text);
writer.WriteAttributeString("password", AccountList.Items[i].Text);
writer.WriteAttributeString("status", AccountList.Items[i].Text);
writer.WriteEndElement();
}
writer.WriteEndElement();
writer.Close();
}