How can add question from XML file into a list type

Hi i want to try to put my atribute from xml file into a list data structure. But i don’t have ideea how to make this… I try to create a class, but i don’t know how can i implement my query in list This is my code:

using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Security.Cryptography.X509Certificates; using System.Xml.Linq;  namespace Lista {         class Program         {                 List<QuestionList> test = new List<QuestionList>();                 static void Main(string[] args)                 {                         var xml = XDocument.Load("questions.xml");                         var query = from index in xml.Descendants("question")                                     where (string)index.Attribute("TypeQuestion") == "eazy"                                     select index;                          foreach(var x in query)                         {                                 Console.WriteLine($"{x}");                         }                 }                 class QuestionList                 {                         public string QuestionName;                         public string answers;                         public string rightIndex;                         public string TypeQuestion;                  }         } } 

xml file:

<QuestionsList>             <question>                 <name>Question 1</name>                 <answers>a1,a2,a3,a4</answers>                 <rightIndex>3</rightIndex>                 <TypeQuestion>eazy</TypeQuestion>             </question>             <question>                 <name>Question 2</name>                 <answers>a1,a2,a3,a4</answers>                 <rightIndex>3,4</rightIndex>                 <TypeQuestion>eazy</TypeQuestion>             </question>             <question>                 <name>Question 3</name>                 <answers>a1,a2,a3,a4</answers>                 <rightIndex>1</rightIndex>                 <TypeQuestion>eazy</TypeQuestion>             </question>             <question>                 <name>Question 4</name>                 <answers>a1,a2,a3,a4</answers>                 <rightIndex>2</rightIndex>                 <TypeQuestion>mediu</TypeQuestion>             </question>             <question>                 <name>Question 5</name>                 <answers>a1,a2,a3,a4</answers>                 <rightIndex>2,4</rightIndex>                 <TypeQuestion>mediu</TypeQuestion>             </question>             <question>                 <name>Question 6</name>                 <answers>a1,a2,a3,a4,a5</answers>                 <rightIndex>5</rightIndex>                 <TypeQuestion>mediu</TypeQuestion>             </question>             <question>                 <name>Question 7</name>                 <answers>a1,a2</answers>                 <rightIndex>1</rightIndex>                 <TypeQuestion>mediu</TypeQuestion>             </question>         </QuestionsList> 

If i try :

var xml = XDocument.Load("questions.xml"); var query = from index in xml.Descendants("question")             select index;  foreach(var x in query) {         Console.WriteLine($"{x}"); } 

I get content but if i try to select all question how has TypeQuestion "eazy" don’t work.

Add Comment
1 Answer(s)

<TypeQuestion> is an element and not an attribute:

var query = from index in xml.Descendants("question")             where (string)index.Element("TypeQuestion") == "eazy"             select index; 

And to get the name of the question from the XElement it’s x.Element("name").Value.

Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.