使用 XmlSerializer 将数据添加到隔离存储中的现有 XML 文件
我正在为教师制作一个考勤应用程序作为学校项目,但在更新独立存储中的 xml 文件时遇到问题。我正在使用 LINQ to XML 将自定义 xml 文件中的学生姓名解析到列表框。每个列表框项目都有一个复选框和学生的姓名。然后,我使用 XmlSerializer 将选定的学生以及当前日期存储到isolatedStorage。当我检索存储的 xml 文件时,一切似乎都正常,但当我想添加更多学生时,它只是覆盖以前存储的文件,而不添加到其中。如何修复此错误,以便将其添加到现有 xml 文件而不是覆盖它?
以下是我的保存和加载类:
- 将日期和名称保存到isolatedStorage: http://pastebin.com/2hjmna0a
- 将日期加载到列表:http://pastebin.com/r7sv77C0
- 将名称加载到列表:http:// astebin.com/jLBVyjFS
我感谢您给我的所有帮助。谢谢!
I'm making an attendance app for teachers as a school project and I'm having problems with updating the xml file in isolated storage. I'm parsing the names of the students to a listbox from a custom xml file using LINQ to XML. Each listbox item has a checkbox and the name of the student. I then store the selected students to IsolatedStorage along with the current date using XmlSerializer. When I retrieve the stored xml file everything seems to be working but when I want to add more students, it just overwrites the previously stored file and doesn't add to it. How can I fix this bug so that it adds to the existing xml file instead of overwriting it?
Here are my save and load classes:
- Saving the Date and Names to IsolatedStorage: http://pastebin.com/2hjmna0a
- Loading Dates to a List: http://pastebin.com/r7sv77C0
- Loading Names to a List: http://pastebin.com/jLBVyjFS
I appreciate all the help you can give me. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不是在我的开发机器上为您提供示例,但假设您的序列化文件是学生列表,请尝试以下方法。
1) 将已有的文件反序列化回对象列表。
2) 将学生添加到反序列化列表中。
例如
MyList.Add(new Student { Name = "Charlie Smith" });
或
MyList.AddRange(MyListOfNewStudentsToAdd)
3) 将整个列表(带有新添加的数据)序列化回文件。
那应该有效。
Not on my dev machine to give you an example, but assuming your serialised file is a list of students, try the following methodology.
1) Deserialise the file you already have back into a list of objects.
2) Add the students to the deserialised list.
eg
MyList.Add(new Student { Name = "Charlie Smith" });
or
MyList.AddRange(MyListOfNewStudentsToAdd)
3) Serialise the whole list (with newly added data) back to the file.
That should work.