简体   繁体   English

C#(WP8)中的数据绑定XML

[英]Databinding XML in C# (WP8)

I'm not sure if this is correct, but trying to learn MVVM, how it works, etc. 我不确定这是否正确,但是尝试学习MVVM,如何工作等。

Currently, the example used to load the data is: 当前,用于加载数据的示例为:

  this.SavedItems.Add(new SavedBoard() { ID= "1098", UserDescription = "Test" });

I want to parse XML and load data from there. 我想解析XML并从那里加载数据。

This is the c# code I've been trying but doesn't seem to work: 这是我一直在尝试的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);

And this is the XML file: 这是XML文件:

<Stops>
    <Stop>
        <ID>1022</ID>
        <UserDescription>Test</UserDescription>
    </Stop>
    <Stop>
        <ID>1053</ID>
        <UserDescription>Test1045</UserDescription>
    </Stop>
</Stops>

Where am I going wrong? 我要去哪里错了? I also get an error Error "Could not find an implementation of the query pattern for source type 'System.Collections.Generic.IEnumerable'. 'Select' not found. Are you missing a reference or a using directive for 'System.Linq'?" 我还收到一个错误错误“找不到源类型'System.Collections.Generic.IEnumerable'的查询模式的实现。找不到'Select'。您是否缺少对'System.Linq'的引用或using指令?”

Though I'm thinking the error isn't the reason it's not working, but rather the code logic itself. 尽管我认为错误不是无法正常运行的原因,而是代码逻辑本身。

Thanks in advance! 提前致谢!

Use doc.Descendants("Stop") (or doc.Root.Elements("Stop") ) instead of Stops , and include the System.Linq namespace with adding: using System.Linq; 使用doc.Descendants("Stop") (或doc.Root.Elements("Stop") )代替Stops ,并在System.Linq命名空间中添加以下using System.Linq;using System.Linq; top of your code. 代码的顶部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM