Hello everyone!
I would like to create a quiz and wonder what is the best way.
1. Using a textfile to read in the questions/answer
2. Using radio buttons
3. Or something else you suggest?
Ps: I have lot of questions in the quiz
Thank you
Hello everyone!
I would like to create a quiz and wonder what is the best way.
1. Using a textfile to read in the questions/answer
2. Using radio buttons
3. Or something else you suggest?
Ps: I have lot of questions in the quiz
Thank you
I would consider an XML format.
<?xml version="1.0" encoding="utf-8"?>
<Quiz>
<Question>
<QuestionText>What date was time travel invented?</QuestionText>
<Answer>July 4, 1776</Answer>
<Answer>December 7, 1941</Answer>
<Answer>August 16, 1977</Answer>
<Answer Correct="true">November 5, 1955</Answer>
</Question>
<Question>
<QuestionText>How much power is required for the flux capacitor to work?</QuestionText>
<Answer>4 Palpatines</Answer>
<Answer>3 Vaders</Answer>
<Answer Correct="true">1.21 Gigawatts</Answer>
<Answer>UNLIMITED</Answer>
</Question>
</Quiz>
You can use XML deserialization or LINQ to XML to easily read the data into an appropriate structure and then use it to control the quiz interface.
[Editing in a usage example]
Based on the aforementioned schema, here is a usage example (LINQ to XML):
Imports System.Linq
Imports System.Xml.Linq
Module Module1
Sub Main()
Dim xmlFile As String = "C:\Temp\quiz.xml"
Dim document As XDocument = XDocument.Load(xmlFile)
Dim query = From question In document.Descendants("Question") _
Select New With _
{ _
.QuestionText = question.Element("QuestionText").Value, _
.Answers = (From answer In question.Descendants("Answer") _
Select New With _
{ _
.AnswerText = answer.Value, _
.AnswerCorrect = If(answer.Attribute("Correct") Is Nothing, False, Boolean.Parse(answer.Attribute("Correct").Value)) _
}).ToList() _
}
For Each question In query
Console.WriteLine(question.QuestionText)
For Each answer In question.Answers
Console.WriteLine("{0}{1}{2}", answer.AnswerCorrect, vbTab, answer.AnswerText)
Next
Console.WriteLine(String.Empty)
Next
Console.Read()
End Sub
End Module
using System;
using System.Linq;
using System.Xml.Linq;
class Program
{
static void Main(string[] args)
{
string xmlFile = @"C:\Temp\quiz.xml";
XDocument document = XDocument.Load(xmlFile);
var query = from question in document.Descendants("Question")
select new
{
QuestionText = question.Element("QuestionText").Value,
Answers = (from answer in question.Descendants("Answer")
select new
{
AnswerText = answer.Value,
AnswerCorrect = answer.Attribute("Correct") != null ? bool.Parse(answer.Attribute("Correct").Value) : false
}).ToList()
};
foreach (var question in query)
{
Console.WriteLine(question.QuestionText);
foreach (var answer in question.Answers)
{
Console.WriteLine("{0}\t{1}", answer.AnswerCorrect, answer.AnswerText);
}
Console.WriteLine(string.Empty);
}
Console.Read();
}
}
And the output in the console:
What date was time travel invented?
False July 4, 1776
False December 7, 1941
False August 16, 1977
True November 5, 1955
How much power is required for the flux capacitor to work?
False 4 Palpatines
False 3 Vaders
True 1.21 Gigawatts
False UNLIMITED
thank you, that's a good idea. unfortunately i am not familar at all with zml. but thanks anyway
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.