我在C#中創建了一個測驗作爲控制檯應用程序。C#將XML元素讀取到2個單獨的列表中
我有一個XML文件,其中包含a)問題b)答案和c)不正確的答案。
我可以從我的XML文件中讀取問題。
但是我無法弄清楚我需要爲每個隨機生成的閱讀問題關聯不正確和正確的答案。
這是我的XML文件的副本。
<?xml version="1.0" encoding="utf-8"?>
<Question xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<theQuestion>How many players in a football team?</theQuestion>
<answerA>12</answerA>
<answerB>10</answerB>
<answerC>20</answerC>
<answerD>11</answerD>
<correctAnswer>11</correctAnswer>
<theQuestion>How many minutes in a football game?</theQuestion>
<answerA>90</answerA>
<answerB>45</answerB>
<answerC>60</answerC>
<answerD>77</answerD>
<correctAnswer>90</correctAnswer>
</Question>
這裏是我的代碼部分:
ProcessData data = new ProcessData();
//load questions from XML file and store in list
var questions = data.LoadQuizQuestions();
//create a question object
Question q = new Question();
//get a question randomly generated from questions list
int index = new Random().Next(questions.Count);
//display the randomly generated question
Console.WriteLine(questions[index]);
Console.ReadLine();
這裏是我的LoadQuizQuestions()
public List<string> LoadQuizQuestions()
{
//create empty list to store quiz questions read in from file
List<string> questions = new List<string>();
//load questions from file into list
questions =
XDocument.Load(@"C:\Development\Learning\Files\qsFile.xml").Descendants("theQuestion").Select(o => o.Value).ToList();
//return list of questions
return questions;
}
我想,當每次隨機抽題顯示相應的回答這個問題也會顯示,並且「正確答案」被讀入一個變量,我可以檢查用戶輸入。
請幫助我瞭解,我知道我靠近釘這個:-)
謝謝
更好地展示您的c OQ for LoadQuizQuestions – ilans
爲什麼不使用xmlnode來查找答案狀態 – Developer
ilans-LoadQuizQuestions只是從XML文件中只讀取問題 – xirokx