locked
Convert List to collections

    Question

  • dear team,

      i am using xml as a database. i am developing a Quiz app. i have doubt in the following code

    string peopleXMLPath = System.IO.Path.Combine(Package.Current.InstalledLocation.Path, "Assets/Questions.xml");
                XDocument loadedData = XDocument.Load(peopleXMLPath);
    
                var data = from query in loadedData.Descendants("question")
                           select new Item
                           {
                               Question = (string)query.Element("Ques"),
                               Hint1 = (string)query.Element("Hint1"),
                               Hint2 = (string)query.Element("Hint2"),
                               Result = (string)query.Element("result") 
                           };

    i donot know how to assign the data in Collection. 

    kindly help me


                
    • Edited by mann madhan1 Thursday, August 14, 2014 6:39 PM spelling
    Thursday, August 14, 2014 6:30 PM

Answers

  • Hi mann madhan1,

    Try following code should help:

    string peopleXMLPath = System.IO.Path.Combine(Package.Current.InstalledLocation.Path, "Assets/Questions.xml");
                XDocument loadedData = XDocument.Load(peopleXMLPath);
    
                var data = (from query in loadedData.Descendants("question")
                           select new Item
                           {
                               Question = (string)query.Element("Ques"),
                               Hint1 = (string)query.Element("Hint1"),
                               Hint2 = (string)query.Element("Hint2"),
                               Result = (string)query.Element("result") 
                           }).toList();

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Friday, August 15, 2014 6:37 AM
    Moderator
  • the following coding is working 

    Item item=new item();
    string peopleXMLPath = System.IO.Path.Combine(Package.Current.InstalledLocation.Path, "Assets/Questions.xml");
                XDocument loadedData = XDocument.Load(peopleXMLPath);
                
                var data = from query in loadedData.Descendants("question")
                           where (string)query.Element("sno") == im
                           select new Item
                           {
    
                               Question = (string)query.Element("Ques"),
                               Hint1 = (string)query.Element("Hint1"),
                               Hint2 = (string)query.Element("Hint2"),
                               Result = (string)query.Element("result"),
                              Content =(string)query.Element("Image")
                           };
                
                foreach (var i in data)
                {
                    item.Question = i.Question;
                    item.Hint1 = i.Hint1;
                    item.Hint2 = i.Hint2;
                    item.Result = i.Result;
                    item.SetImage(_baseUri, i.Content);
                    Collection.Add(item);
                }
    

    • Marked as answer by mann madhan1 Friday, August 15, 2014 1:22 PM
    Friday, August 15, 2014 1:22 PM

All replies

  • Hi mann madhan1,

    Try following code should help:

    string peopleXMLPath = System.IO.Path.Combine(Package.Current.InstalledLocation.Path, "Assets/Questions.xml");
                XDocument loadedData = XDocument.Load(peopleXMLPath);
    
                var data = (from query in loadedData.Descendants("question")
                           select new Item
                           {
                               Question = (string)query.Element("Ques"),
                               Hint1 = (string)query.Element("Hint1"),
                               Hint2 = (string)query.Element("Hint2"),
                               Result = (string)query.Element("result") 
                           }).toList();

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Friday, August 15, 2014 6:37 AM
    Moderator
  • the following coding is working 

    Item item=new item();
    string peopleXMLPath = System.IO.Path.Combine(Package.Current.InstalledLocation.Path, "Assets/Questions.xml");
                XDocument loadedData = XDocument.Load(peopleXMLPath);
                
                var data = from query in loadedData.Descendants("question")
                           where (string)query.Element("sno") == im
                           select new Item
                           {
    
                               Question = (string)query.Element("Ques"),
                               Hint1 = (string)query.Element("Hint1"),
                               Hint2 = (string)query.Element("Hint2"),
                               Result = (string)query.Element("result"),
                              Content =(string)query.Element("Image")
                           };
                
                foreach (var i in data)
                {
                    item.Question = i.Question;
                    item.Hint1 = i.Hint1;
                    item.Hint2 = i.Hint2;
                    item.Result = i.Result;
                    item.SetImage(_baseUri, i.Content);
                    Collection.Add(item);
                }
    

    • Marked as answer by mann madhan1 Friday, August 15, 2014 1:22 PM
    Friday, August 15, 2014 1:22 PM