我在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