我不确定这是否正确,但是试图学习MVVM,它是如何工作的等等。
目前,用于加载数据的示例是:
this.SavedItems.Add(new SavedBoard() { ID= "1098", UserDescription = "Test" });
我想解析XML并从那里加载数据。
这是我一直在尝试的c#代码,但似乎不起作用:
XDocument doc = XDocument.Load("savedstops.xml");
var data = from query in doc.Descendants("Stops")
select new SavedBoard
{
ID = query.Element("ID").Value,
UserDescription = query.Element("UserDescription").Value
};
this.SavedItems.Add(data);
这是XML文件:
<Stops>
<Stop>
<ID>1022</ID>
<UserDescription>Test</UserDescription>
</Stop>
<Stop>
<ID>1053</ID>
<UserDescription>Test1045</UserDescription>
</Stop>
</Stops>
我哪里错了?我也收到错误错误“无法找到源类型'System.Collections.Generic.IEnumerable'的查询模式的实现。'选择'找不到。是否缺少'System.Linq'的引用或using指令?”
虽然我认为错误不是它不起作用的原因,而是代码逻辑本身。
提前致谢!
答案 0 :(得分:1)
使用doc.Descendants("Stop")
(或doc.Root.Elements("Stop")
)代替Stops
,并添加System.Linq
命名空间,并添加:using System.Linq;
代码顶部。