Note that there are some explanatory texts on larger screens.

plurals
  1. POCan read this xml into my class structure
    primarykey
    data
    text
    <p>I need to read this xml into a class. I'm new to LINQ to XML.</p> <pre><code>&lt;quiz&gt; &lt;step id="1"&gt; &lt;question id="1"&gt; &lt;text&gt;What is the world’s tallest tower?&lt;/text&gt; &lt;answers&gt; &lt;answer id="1" value="0"&gt;Eiffel Tower&lt;/answer&gt; &lt;answer id="2" value="0"&gt;Petronas Tower&lt;/answer&gt; &lt;answer id="3" value="0"&gt;Canton Tower&lt;/answer&gt; &lt;answer id="4" value="1"&gt;Tokyo Skytree &lt;/answer&gt; &lt;/answers&gt; &lt;/question&gt; &lt;/step&gt; &lt;/quiz&gt; </code></pre> <p>I've created these two classes, but I'm not sure about how to read the info in easily. My head is wrecked from it.</p> <pre><code>public class QuizQuestion { public int StepId { get; set; } public int QuestionId { get; set; } public string QuestionText { get; set; } public List&lt;QuizAnswer&gt; Answers { get; set; } } public class QuizAnswer { public int AnswerId { get; set; } public int CorrectAnswer { get; set; } public String AnswerText { get; set; } } </code></pre> <p>I've tried this, not sure if it's right</p> <pre><code>var quizQuestions = new List&lt;QuizQuestion&gt;(); _cacheLock.EnterWriteLock(); try { XDocument xmlDoc = XDocument.Load(_questionsFilePath); XDocument data = XDocument.Load(_questionsFilePath); quizQuestions = (from c in data.Descendants("quiz") orderby c.Attribute("question") select new QuizQuestion() { StepId = Convert.ToInt32(c.Attribute("Id").Value), QuestionId = Convert.ToInt32(c.Attribute("Id").Value), QuestionText = c.Value }).ToList(); foreach (QuizQuestion quiz in quizQuestions) { quiz.Answers = (from c in data.Descendants("quiz") orderby c.Attribute("question") where Convert.ToInt32(c.Attribute("Id").Value) == 1 select new QuizAnswer() { AnswerId = Convert.ToInt32( c.Attribute("id").Value), AnswerText = c.Value, CorrectAnswer = Convert.ToInt32( c.Attribute("value").Value) }).ToList(); } } catch (NullReferenceException e) { } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload