简体   繁体   中英

Databinding XML in C# (WP8)

I'm not sure if this is correct, but trying to learn MVVM, how it works, etc.

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.

This is the c# code I've been trying but doesn't seem to work:

        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:

<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'?"

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; top of your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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