Click here to Skip to main content
15,861,168 members
Articles / Web Development / ASP.NET
Article

Online Quiz

Rate me:
Please Sign up or sign in to vote.
4.73/5 (51 votes)
27 Aug 20027 min read 822.3K   33.2K   194   119
Create a multiple-choice quiz using XML DOM and XPath

Sample Image - xmlquiz_snapshot.gif

Introduction

The idea to write an online quiz came up when I was looking for an XML tutorial. I visited W3Schools.com and found not only the tutorial I was looking for, but more interestingly was an online quiz. I took the 20-questions quiz once, and hey! I felt great about it. I wonder why there are just few web sites offering online quiz like that.

A quiz is a great way to test your knowledge. An online quiz is a great addition to your web site that could keep your visitors glued for a few more minutes.

Download the demo project and try it. It is a 10-question quiz to challenge your knowledge about Australian geography. Don't worry! All data is kept in a clear, human-readable XML document, so you could easily peek for answers.

The Script Explained

I will not go through a very detail discussion about the script, but rather highlight several areas in the script. Once you get the whole idea about the script, then it is easy to modify or extend the script to suit your requirements.

Recursive Script

There is only one aspx script to do various tasks in the online quiz. It is a recursive script: a script that 'calls' itself over and over again until a certain condition is reached. Precisely, the script does not call itself, but posts form data to itself. This process is known as post back.

As the script posts back to itself continuously over the duration of the quiz, we could say that it has many states. The first state is to initialize several essential variables, count the total question, and record the quiz start time. Then, in the first and each following state, the script displays a multiple choice question to challenge user (see the snapshot above). A user answering the question will trigger onClick event, forcing a post-back, and move the script to the next state. In the next state, the script will run a subroutine associated with the event to check the answer and display the next multiple question. The recursive flow repeats again and again, until the last question is processed, where at this point a result is displayed.

The following activity diagram represents the recursive flow of the online quiz script. 

Image 2

Maintaining State

The online quiz script needs to maintain state of its variables. There are a bunch of alternatives to do so. The most advanced way is to use the session object, and the conventional way is to use hidden inputs or a QueryString. ASP.NET introduces another alternative called 'state bag'. Unlike the session object, state bag is not persisted over the whole user session, but is brought forward from one page to another page just like hidden inputs and querystring. However, it is superior to hidden inputs and QueryStrings, since it can accept more data types and the content has been encoded into a single string and therefore is not easy to tamper with.

Storing value into state bag:

VB
ViewState("TotalQuestion") = intTotalQuestion

Getting value from state bag:

VB
intTotalQuestion = ViewState("TotalQuestion")

The following is a list of variables to be kept in the state bag:

Variable Name State Bag Name Data Type Comments
intTotalQuestion TotalQuestion int Keeps the total question in the quiz. The value is populated in the first state of the quiz and remains constants over the duration of quiz.
intScore Score int Keeps the number of correct answer.
intQuestionNo QuestionNo int Holds the last question number the user attempted.
arrAnswerHistory AnswerHistory arraylist of int Records answers in the quiz. It will record 0 (zero) if the answer is correct, otherwise record the selectedindex of the radio buttons.
(none) CorrectAnswer int Holds the correct answer of previous question. It is made available in the next state when the answer is checked for correctness.
(none) StartTime date Holds the start time of the quiz. It is used to calculate the time spent in the quiz.

 

XML Data

Data for the online quiz is kept in an XML document named quiz.xml, which is validated using an XML schema named quiz.xsd. A valid XML document consists of a root element called quiz, which has at least one element called mchoice (short for multiple-choice). Each mchoice element has one question child element, and two or more answer child elements. The answer element may have the correct attribute with possible value of either yes or no. In fact, you should supply the correct attribute with a value of yes to one of the answers in the same mchoice, otherwise there will be no correct answer for the question.

quiz.xml:

XML
<?xml version="1.0" encoding="UTF-8"?>
<quiz xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="quiz.xsd">
  <mchoice>
    <question>What is the capital city of Australia ?</question>
    <answer>Sydney</answer>
    <answer correct="yes">Canberra</answer>
    <answer>Melbourne</answer>
    <answer>Gold Coast</answer>
  </mchoice>
  <mchoice>
    <question>Which city has an extensive tram network?</question>
    <answer>Sydney</answer>
    <answer correct="yes">Melbourne</answer>
    <answer>Adelaide</answer>
    <answer>Ballarat</answer>
  </mchoice>
</quiz>

It is possible to insert HTML tags within the XML data, therefore the quiz may contain decorated texts, images, links, etc. instead of plain text. Just make sure to enclose the HTML tags with CDATA block, so the XML document is still valid. Look at the following example:

XML
  <mchoice>
    <question><![CDATA[Which of the following is <u>NOT</u> 
Australian native animals?]]></question>
    <answer>Kangaroo</answer>
    <answer correct="yes">Penguin</answer>
    <answer>Koala</answer>
    <answer>Wombat</answer>
  </mchoice>

quiz.xsd:

XML
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" attributeFormDefault="unqualified">
  <xs:element name="quiz">
    <xs:complexType>
      <xs:choice>
        <xs:element name="mchoice" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="question" type="xs:string"/>
              <xs:element name="answer" minOccurs="2" maxOccurs="unbounded">
                <xs:complexType>
                  <xs:simpleContent>
                    <xs:extension base="xs:string">
                      <xs:attribute name="correct" use="optional">
                        <xs:simpleType>
                          <xs:restriction base="xs:string">
                            <xs:enumeration value="yes"/>
                            <xs:enumeration value="no"/>
                          </xs:restriction>
                        </xs:simpleType>
                      </xs:attribute>
                    </xs:extension>
                  </xs:simpleContent>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:choice>
    </xs:complexType>
  </xs:element>
</xs:schema>

The online quiz script does not validate the XML document against the XML Schema for several reasons. First, it is a resource intensive process, forcing

XMLTextReader
to go through each element and attribute in the XML document. Second, we need to validate the XML document just once after it has been updated, instead of every time we load the file. To validate the XML document, you could write a separate aspx script or use a third-party tool and run them manually every after you finish updating the XML document.


XML Document Object Model

XML Document Object Model (DOM) is a tree-like structure that represents every node of an XML document based on the hierarchical relationship with its parent nodes and child nodes. The DOM allows us to navigate and manipulate XML document in more logical way.

To build an XML DOM, we use the XMLDocument class. The XMLDocument class itself extends the XMLNode class, therefore many of its properties and methods are inherited from XMLNode. While XMLNode's methods and properties apply to a specific node in an XML document, methods and properties of XMLDocument apply to the whole XML document.

The following code create an instance of XMLDocument and build XML DOM from quiz.xml:

VB
Dim xDoc as XMLDocument = New XMLDocument()
xDoc.Load(Server.MapPath("quiz.xml"))

Addressing a specific node in the XML DOM is a bit tricky. We should navigate our 'pointer' through the DOM starting from its root node. The following code demonstrates how to address the first question of the first multiple choice:

VB
Dim xNode as XMLNode

'Goto the first/base element in the file, which is <?xml ?>
xNode = xDoc.DocumentElement

'Goto the next sibling of current node, which is <quiz>
xNode = xNode.NextSibling

'Goto the first child of current node, which is <mchoice>
xNode = xNode.FirstChild

'Goto the first child of current node, which is <quiz>
xNode = xNode.FirstChild

'Print the content of current node
Response.Write("The question is: " & xNode.InnerHtml)

It is definitely a tedious task, particularly if you want to address nodes located at a very low level in the hierarchy. Luckily, we can utilize the XPath language to more directly address a specific node or a group of nodes. If you are unfamiliar with XPath, it will be briefly explained in the next section.

The SelectNodes method of the XMLNode and XMLDocument class' accepts an XPath string and returns a collection of XMLNode objects, called

XMLNodeList
. Another method, SelectSingleNode, does just the same thing but return only a single XMLNode object.

VB
'Using SelectNodes to address all answers of the first multiple choice
Dim xNodeList As XMLNodeList
Dim i as Integer
xNodeList = xDoc.SelectNodes("/quiz/mchoice[1]/answer")
For i = 0 to xNodeList.Count-1
  Response.Write("<p>" & xNodeList.Item(i).InnerHtml)
Next


'Using SelectSingleNode to select the first question of the first multiple choice
Dim xNode as XMLNode
xNode = xDoc.SelectSingleNode("/quiz/mchoice[1]/question")
Response.Write("The question is: " & xNode.InnerHtml)


XPath

XPath is a language to address specific nodes in an XML document. It could address a single node or a group of nodes by describing its hierarchy relationship in a string, therefore it is often called an XPath string. If you are familiar with file path or URL, then the concept of XPath is nothing new.

Read the XPath string from left to right and you will be able to figure out the node it is addressing. Except for several conditions and functions, XPath is actually easy to use. The following table demonstrates some usage of XPath against quiz.xml:

XPath String Result
/quiz Select the root node of XML including all elements it contains. 
/quiz/mchoice Select all mchoice child elements of the quiz
/quiz/mchoice[1] Select the first mchoice (multiple choice) child element of the quiz
/quiz/mchoice[1]/question Select all questions of the first multiple choice of the quiz
/quiz/mchoice[1]/answer[4] Select the fourth answer of the first multiple choice of the quiz
/quiz/mchoice[1]/answer[4]/@correct Select 'correct' attribute of the fourth answer of the first multiple choice of the quiz

XPath contains a lot more surprises. If you are interested enough to explore more, check out the XPath Tutorial at W3CSchools.

Conclusions

This article presented an online quiz as a tool to add interactivity to your web site. It also explored several topics like recursive scripts, navigating and manipulating XML documents using the XML DOM, a glimpse of the XPath language, and a brief discussion about state maintenance.

Links

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Singapore Singapore
Currently, he is in Singapore doing a contract work for a multinational company. During weekend, he is busy exploring Singapore with his lovely fiance to find the best food.

Comments and Discussions

 
Questionpls anybody ??I need help Pin
Paromita20-Mar-08 9:03
Paromita20-Mar-08 9:03 
QuestionEXPERT HELP-How to make the quiz software? Pin
Paromita12-Mar-08 22:55
Paromita12-Mar-08 22:55 
GeneralAssessment Tool Pin
Poven4-Dec-07 18:21
Poven4-Dec-07 18:21 
GeneralNice quiz...a couple of additions Pin
GemmaBlckbrn22-Oct-07 8:41
GemmaBlckbrn22-Oct-07 8:41 
Generalss Pin
Nagendrababu23-Jul-07 0:18
Nagendrababu23-Jul-07 0:18 
GeneralMedia player in Asp.net VB Pin
leonardofreitas86-Jul-07 1:09
leonardofreitas86-Jul-07 1:09 
Generalsimple way on doing web-based online examination Pin
ms Liza24-Jun-07 20:45
ms Liza24-Jun-07 20:45 
GeneralPlease help me.... Pin
P.K.Pattnaik14-May-07 23:42
P.K.Pattnaik14-May-07 23:42 
Dear sir,

How can i add a new question with answers to the XML file you have used in the online quiz applicvation ? Can I add the questions directly using WriteXml() or some other way should be followed.

Please help me in this regard.

Waiting for positive responce..


Prasanta
Generalcode behind page Pin
gbcheese111-May-07 4:03
gbcheese111-May-07 4:03 
Questionsilly me Pin
sxhutch4-Apr-07 8:20
sxhutch4-Apr-07 8:20 
Generalgreat work Pin
Dimitar Voynov17-Feb-07 10:43
Dimitar Voynov17-Feb-07 10:43 
QuestionXPathNavigator xNav problem Pin
inrakeshworld4-Feb-07 21:11
inrakeshworld4-Feb-07 21:11 
Questionhow to pick questions randomly from xml file or Sql server Database? Pin
irkahc26-Dec-06 23:43
irkahc26-Dec-06 23:43 
AnswerRe: how to pick questions randomly from xml file or Sql server Database? [modified] Pin
Dr.Zeus4-Feb-07 12:05
Dr.Zeus4-Feb-07 12:05 
GeneralSample online test application Pin
irkahc26-Dec-06 21:44
irkahc26-Dec-06 21:44 
GeneralASP.net 2.0 Pin
smartinezpr12-Dec-06 8:00
smartinezpr12-Dec-06 8:00 
GeneralRe: ASP.net 2.0 Pin
OrucKenan25-May-11 5:57
professionalOrucKenan25-May-11 5:57 
GeneralExcellent article and sample code Pin
Robert J. Lorenzini21-Mar-06 14:50
Robert J. Lorenzini21-Mar-06 14:50 
QuestionWeb-based Online Examination System Pin
aneebor17-Mar-06 3:41
aneebor17-Mar-06 3:41 
AnswerRe: Web-based Online Examination System Pin
suresh pandu19-Mar-06 17:32
suresh pandu19-Mar-06 17:32 
GeneralRe: Web-based Online Examination System Pin
aneebor19-Mar-06 18:10
aneebor19-Mar-06 18:10 
GeneralRe: Web-based Online Examination System Pin
suresh pandu30-Mar-06 19:43
suresh pandu30-Mar-06 19:43 
GeneralRe: Web-based Online Examination System Pin
aneebor30-Mar-06 23:45
aneebor30-Mar-06 23:45 
GeneralRe: Web-based Online Examination System Pin
hellraisr882-Mar-08 20:02
hellraisr882-Mar-08 20:02 
GeneralAdd Image Pin
LovelyHelp28-Feb-06 5:31
LovelyHelp28-Feb-06 5:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.