Guys ,
I found an very resourceful implementation of online quiz .
Online Quiz[
^]
But the problem is i wish to reuse this code and written in C# with code behind form.
I have been trying to convert but end up with ton of error. will be graceful if any one can figure it out.
I be deeply appreciate .
the code behind ,quiz.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Xml.XPath;
public partial class _Default : System.Web.UI.Page
{
string strXmlFilePath = Server.MapPath("quiz.xml");
XPathDocument xDoc = new XPathDocument(strXmlFilePath);
XPathNavigator xNav = xDoc.CreateNavigator;
XPathNodeIterator xNodeIterator;
int intTotalQuestion;
int intQuestionNo = 1;
int intScore = 0;
ArrayList arrAnswerHistory = new ArrayList();
public void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
intTotalQuestion = xNav.Select("/quiz/mchoice").Count;
ViewState["StartTime"] = DateTime.Now;
ShowQuestion(intQuestionNo);
}
}
public void btnSubmit_Click(object src, EventArgs e)
{
intTotalQuestion = ViewState["TotalQuestion"];
intQuestionNo = ViewState["QuestionNo"];
intScore = ViewState["Score"];
arrAnswerHistory = ViewState["AnswerHistory"];
if (rblAnswer.SelectedItem.Value == ViewState["CorrectAnswer"])
{
intScore += 1;
arrAnswerHistory.Add(0);
}
else
{
arrAnswerHistory.Add(rblAnswer.SelectedItem.Value);
}
if (intQuestionNo == intTotalQuestion)
{
QuizScreen.Visible = false;
ResultScreen.Visible = true;
ShowResult();
}
else
{
QuizScreen.Visible = true;
ResultScreen.Visible = false;
intQuestionNo += 1;
ShowQuestion(intQuestionNo);
}
}
public void ShowQuestion(int intQuestionNo)
{
string strXPath = null;
int intLoop = 0;
TimeSpan objTimeSpent = default(TimeSpan);
strXPath = "/quiz/mchoice[" + intQuestionNo.ToString() + "]";
xNodeIterator = xNav.Select(strXPath + "/question");
xNodeIterator.MoveNext();
lblQuestion.Text = intQuestionNo.ToString() + ". " + xNodeIterator.Current.Value;
xNodeIterator = xNav.Select(strXPath + "/answer");
rblAnswer.Items.Clear();
intLoop = 0;
while (xNodeIterator.MoveNext())
{
intLoop += 1;
rblAnswer.Items.Add(new ListItem(xNodeIterator.Current.Value, intLoop));
if (xNodeIterator.Current.GetAttribute("correct", "") == "yes")
{
ViewState["CorrectAnswer"] = intLoop;
}
}
lblTotalQuestion.Text = intTotalQuestion;
objTimeSpent = DateTime.Now.Subtract(ViewState["StartTime"]);
lblTimeSpent.Text = objTimeSpent.Minutes.ToString() + ":" + objTimeSpent.Seconds.ToString();
ViewState["TotalQuestion"] = intTotalQuestion;
ViewState["Score"] = intScore;
ViewState["QuestionNo"] = intQuestionNo;
ViewState["AnswerHistory"] = arrAnswerHistory;
}
public void ShowResult()
{
string strResult = null;
int intCompetency = 0;
int intLoop = 0;
string strXPath = null;
TimeSpan objTimeSpent = default(TimeSpan);
objTimeSpent = DateTime.Now.Subtract(ViewState["StartTime"]);
strResult = "<center>";
strResult += "<h3>Quiz Result</h3>";
strResult += "<p>Points: " + intScore.ToString() + " of " + intTotalQuestion.ToString();
strResult += "<p>Your Competency: " + Conversion.Int(intScore / intTotalQuestion * 100).ToString() + "%";
strResult += "<p>Time Spent: " + objTimeSpent.Minutes.ToString() + ":" + objTimeSpent.Seconds.ToString();
strResult += "</center>";
strResult += "<h3>Quiz Breakdown:</h3>";
for (intLoop = 1; intLoop <= intTotalQuestion; intLoop++)
{
strXPath = "/quiz/mchoice[" + intLoop.ToString() + "]";
xNodeIterator = xNav.Select(strXPath + "/question");
xNodeIterator.MoveNext();
strResult += "<b>" + intLoop.ToString() + ". " + xNodeIterator.Current.Value + "</b><br>";
if (arrAnswerHistory[intLoop - 1] == 0)
{
strResult += "<font color=\"green\"><b>Correct</b></font><br><br>";
}
else
{
xNodeIterator = xNav.Select(strXPath + "/answer[" + arrAnswerHistory[intLoop - 1].ToString() + "]");
xNodeIterator.MoveNext();
strResult += "<b>You answered:</b> " + xNodeIterator.Current.Value + "<br>";
strResult += "<font color=\"red\"><b>Incorrect</b></font><br><br>";
}
}
lblResult.Text = strResult;
}
}
the quiz.aspx.
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Australian Geography Quiz</title>
</head>
<style>
body {
font-size: 10pt;
font-family: verdana,helvetica,arial,sans-serif;
color:#000000;
background-color:#eeeedd;
}
tr.heading {
background-color:#900B08;
}
.button {
border: 1px solid #000000;
background-color: #ffffff;
}
</style>
<body>
<span id="QuizScreen" runat="server">
<form id="Form2" runat="server">
<table width="100%" border="0" cellpadding="2" cellspacing="0">
<tr class="heading">
<td width="50%"><font color="white"><b>Australian Geography Quiz</b></font></td>
<td width="50%" align="right"><font color="white"><b>www.codeproject.com</b></font></td>
</tr>
<tr>
<td colspan="2">
<b><asp:label id="lblQuestion" runat="server" /></b><br>
<asp:radiobuttonlist
id="rblAnswer"
RepeatDirection="vertical"
TextAlign="right"
RepeatLayout="table"
runat="server" /><br>
<asp:requiredfieldvalidator ID="Requiredfieldvalidator1"
ControlToValidate="rblAnswer"
ErrorMessage="Please pick an answer!"
runat="server" /><br>
<asp:button id="btnSubmit" class="button" text=" Next " onClick="btnSubmit_Click" runat="server" />
</td>
</tr>
<tr class="heading">
<td width="50%"><font color="white"><b>Total <asp:label id="lblTotalQuestion" runat="server" /> questions</b></font></td>
<td width="50%" align="right"><font color="white"><b>Time spent <asp:label id="lblTimeSpent" runat="server" /></b></font></td>
</tr>
</table>
</form>
</span>
<span id="ResultScreen" runat="server">
<asp:label id="lblResult" runat="server" />
</span>
</body>
</html
the quiz.xml originated from
Online Quiz[
^]
<<pre lang="xml">?xml version="1.0" encoding="UTF-8"?>
<!-- 10 question quiz about Australian Geography -->
<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>Launceston is the second largest city in which Australian state?</question>
<answer>Victoria</answer>
<answer>New South Wales</answer>
<answer correct="yes">Tasmania</answer>
<answer>Western Australia</answer>
</mchoice>
<mchoice>
<question>Which state has the famous 'Twelve Apostles' ?</question>
<answer correct="yes">Victoria</answer>
<answer>South Australia</answer>
<answer>New South Wales</answer>
<answer>Western Australia</answer>
</mchoice>
<mchoice>
<question>Which is a popular ski resort in NSW?</question>
<answer correct="yes">Perisher Blue</answer>
<answer>Mt. Buller</answer>
<answer>Mt. Baw-Baw</answer>
<answer>Lake Mountain</answer>
</mchoice>
<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>
<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>
<mchoice>
<question>What is known as 'The Silver City' in Australia?</question>
<answer>Alice Springs</answer>
<answer correct="yes">Broken Hill</answer>
<answer>Ballarat</answer>
<answer>Silverton</answer>
</mchoice>
<mchoice>
<question>In which location the war movie 'Thin Red Line' was taken?</question>
<answer>Anglesea</answer>
<answer>Apollo Bay</answer>
<answer>Margaret River</answer>
<answer>Monkey Mia</answer>
<answer correct="yes">Townsville</answer>
</mchoice>
<mchoice>
<question><![CDATA[Which is <u>NOT</u> true about Uluru ?]]></question>
<answer>It is the world biggest monolith located in the centre of Australian continent</answer>
<answer>It was named 'Ayers Rock' by European explorer William Gosse in 1873</answer>
<answer correct="yes">Aboriginal people encourage tourists to climb Uluru</answer>
<answer>The area contains carvings and paintings by Aboriginal people</answer>
</mchoice>
<mchoice>
<question>What is so special about Longreach?</question>
<answer>The place where a blacksmith named Thomas Hiscock found the first gold that triggerred gold rush</answer>
<answer>The town has an expansive, well-preserved penal colony of Australia's early history</answer>
<answer correct="yes">The first commercial flight by Qantas took from this town in 1921</answer>
<answer>None of these answers are correct</answer>
</mchoice>
</quiz>