使用 XmlSerializer 将数据添加到隔离存储中的现有 XML 文件

发布于 2025-01-05 17:12:58 字数 645 浏览 2 评论 0原文

我正在为教师制作一个考勤应用程序作为学校项目,但在更新独立存储中的 xml 文件时遇到问题。我正在使用 LINQ to XML 将自定义 xml 文件中的学生姓名解析到列表框。每个列表框项目都有一个复选框和学生的姓名。然后,我使用 XmlSerializer 将选定的学生以及当前日期存储到isolatedStorage。当我检索存储的 xml 文件时,一切似乎都正常,但当我想添加更多学生时,它只是覆盖以前存储的文件,而不添加到其中。如何修复此错误,以便将其添加到现有 xml 文件而不是覆盖它?

以下是我的保存和加载类:

我感谢您给我的所有帮助。谢谢!

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:

I appreciate all the help you can give me. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怪异←思 2025-01-12 17:12:58

不是在我的开发机器上为您提供示例,但假设您的序列化文件是学生列表,请尝试以下方法。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文