【发布时间】:2017-11-29 12:19:04
【问题描述】:
您能告诉我如何修复我的代码吗?我尝试了很多方法,例如使用 XmlSerializer,但仍然没有。 代码总是保存列表的最后一项,我不知道如何修复它。 代码:
foreach (ObservableCollection<Person> x in list)
{
XDocument xdoc = new XDocument(
new XDeclaration("1.0", "utf-8", "yes"),
new XElement("Person",
from person in x
select new XElement("Person",
new XElement("Name", person.Name),
new XElement("Surname", person.Surname),
new XElement("Age", person.Age))));
xdoc.Save(path);
}
如果有任何提示,我将不胜感激!
【问题讨论】:
-
list是什么类型?from image select没有意义,你能确定你发布了一个编译的例子吗? -
我之前有自己的代码,但无法理解,所以我将其更改为更透明的示例。所以应该有“从人中选择”人的类型:ObservableCollection
> list -
xdoc.Save(path)。如果路径相同,它将继续覆盖旧文件。因此,如果list中的最后一个ObservableCollection<Person>只有一个元素,您将只能看到文件中的最后一项。