locked
Give data from XML to combobox?

    Question

  • public object query(string s)
            {
                XDocument xml = XDocument.Load(@"XMLSession.xml");
                List<Data> data=(from q in xml.Elements("session").Elements("effort")
                                 where q.Element("title").Value.Equals(s)
                                 select new Data
                                 {
                                     title=q.Element("title").Value,
                                 }
                                     ).ToList();
                return data;
            }
    Giving "title" to on combobox.
    Thursday, April 23, 2015 1:24 PM

Answers

  • Since the query method you have posted returns a List<Data> you should change the return type of the method from object to List<Data>:

            public List<Data> query(string s)
            {
                XDocument xml = XDocument.Load(@"XMLSession.xml");
                List<Data> data = (from q in xml.Elements("session").Elements("effort")
                                   where q.Element("title").Value.Equals(s)
                                   select new Data
                                   {
                                       title = q.Element("title").Value,
                                   }
                                     ).ToList();
                return data;
            }


    You can then set the ItemsSource property of your ComboBox to the list returned from the method:

    var items = query("some title");
    combo1.DisplayMemberPath = "title":
    combo1.ItemsSource = items;
    <ComboBox x:Name="combo1" />

    Make sure that your query method actually returns some items and title is a public property of the Data class:

            public class Data
            {
                public string title { get; set; }
            }


    Then you should see the Data items returned from your method in the ComboBox.

    Hope that helps.

    Please remember to mark helpful posts as answer to close your threads and then start a new thread if you have a new question. Please don't ask several questions in the same thread.

    Thursday, April 23, 2015 3:03 PM