I am currently making a WinForms application where I have a ListView with a bunch of items. The columns are: Name, World, Vocation, Level and Status.
I want to save these to an XML file in this format:
<ArrayOfPerson>
<Person>
<Name>Bobby</Name>
<World>Earth</World>
<Vocation>Blabla</Vocation>
<Level>Blablabla</Level>
<Status>Online</Status>
</Person>
<Person>
<Name>Jeff</Name>
<World>Uranus</World>
<Vocation>Blabla</Vocation>
<Level>Blablabla</Level>
<Status>Offline</Status>
</Person>
</ArrayOfPerson>
And then also be able to load it into the ListView.
My current code looks like this and just saves to a file name "list.xml" in the same folder. Also, the file only outputs it in one line. Instead of nice lines in the XML file (easy to read) it is all mashed together on 1 or 2 lines, which can be quite difficult if I'd want to open the XML file in an editor.
How can I:
-
Use a SaveFileDialog and choose filename for this
-
Put 1 element on each line in the XML file, and not all together
-
Add a OpenFileDialog and load each person into the list again
public class Person { public string Name { get; set; } public string World { get; set; } public string Vocation { get; set; } public string Level { get; set; } public string Status { get; set; } }
private void saveListToolStripMenuItem_Click(object sender, EventArgs e) { List<Person> people = new List<Person>(); for (int i=0; i<characterList.Items.Count; i++) { string nameSave = characterList.Items[i].SubItems[0].Text; string worldSave = characterList.Items[i].SubItems[1].Text; string vocationSave = characterList.Items[i].SubItems[2].Text; string levelSave = characterList.Items[i].SubItems[3].Text; string statusSave = characterList.Items[i].SubItems[4].Text; people.Add(new Person { Name = nameSave, World = worldSave, Vocation = vocationSave, Level = levelSave, Status = statusSave }); } using (var writer = XmlWriter.Create("list.xml")) { var serializer = new XmlSerializer(typeof(List<Person>)); serializer.Serialize(writer, people); } }
I also provide a screenshot of how the list looks:
Aucun commentaire:
Enregistrer un commentaire