Reputation: 605
Piece of code I'm working with here is almost perfect accept I'm doing something wrong with the iteration piece I think. Items from the ListView are not becoming separate and distinct nodes in the XML file. Each subsequent item is becoming a child node of the first item in the ListView. What am I doing wrong? Code as follows:
private void button1_Click(object sender, EventArgs e)
{
System.Xml.XmlTextWriter writer = new System.Xml.XmlTextWriter(Application.StartupPath + "\\database.xml", null);
writer.WriteStartElement("Database");
for (int i = 0; i < listView1.Items.Count; i++)
{
writer.WriteStartElement("Account");
writer.WriteAttributeString("Description", listView1.Items[i].SubItems[0].Text);
writer.WriteAttributeString("Username", listView1.Items[i].SubItems[1].Text);
writer.WriteAttributeString("Password", listView1.Items[i].SubItems[2].Text);
}
writer.WriteEndElement();
writer.Close();
}
Upvotes: 0
Views: 212
Reputation: 160852
You didn't write out the Account
end element - This should do it:
for (int i = 0; i < listView1.Items.Count; i++)
{
writer.WriteStartElement("Account");
writer.WriteAttributeString("Description", listView1.Items[i].SubItems[0].Text);
writer.WriteAttributeString("Username", listView1.Items[i].SubItems[1].Text);
writer.WriteAttributeString("Password", listView1.Items[i].SubItems[2].Text);
writer.WriteEndElement();
}
Upvotes: 1
Reputation: 100527
You don't have WriteEndElement inside you for loop to close "Account node.
Upvotes: 1