Praesidium
asked on
C# Question...
The following code class compiles correctly, it doesn't throw any exceptions, it, however, does not seem to be loading my XML document. It loads... just no questions (it is meant to generate a quiz based on an XML document). Any suggestions?
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls ;
using System.Xml;
using System.Xml.XPath;
namespace Pace
{
/// <summary>
/// Summary description for quiz.
/// </summary>
public class quiz : System.Web.UI.Page{
protected Button btnSubmit;
protected Label lblQuestion;
protected RadioButtonList rblAnswer;
protected Label lblTotalQuestion;
protected Label lblTimeSpent;
protected Label lblResult;
protected Panel QuizScreen;
protected Panel ResultScreen;
XPathNavigator xNav;
XPathDocument xDoc;
XPathNodeIterator xNodeIterator;
System.Collections.ArrayLi st arrAnswerHistory = new ArrayList();
int intTotalQuestion;
int intQuestionNo;
int intScore;
protected override void OnInit(EventArgs e) {
base.OnInit(e);
this.InitializeComponent() ;
string strXmlDoc = "default.xml";
string strXmlFilePath = Server.MapPath("~/media/qu izzes/defa ult.xml" );
XPathDocument xDoc = new XPathDocument(strXmlFilePa th);
XPathNavigator xNav = xDoc.CreateNavigator();
}
private void InitializeComponent() {
this.btnSubmit.Click += new EventHandler(this.btnSubmi t_Click);
}
public static void Main() {
}
private void Page_Load(object sender, System.EventArgs e){
this.Main();
if(!Page.IsPostBack){
intTotalQuestion = xNav.Select("/quiz/mchoice ").Count;
ViewState["StartTime"] = DateTime.Now;
ShowQuestion(intQuestionNo );
}
}
private void btnSubmit_Click(object src, EventArgs e) {
intTotalQuestion = (int)this.ViewState["Total Question"] ;
intQuestionNo = (int)this.ViewState["Quest ionNo"];
intScore = (int)this.ViewState["Score "];
arrAnswerHistory = (System.Collections.ArrayL ist)this.V iewState[" AnswerHist ory"];
if(rblAnswer.SelectedItem. Value == (string)this.ViewState["Co rrectAnswe r"]) {
intScore += 1;
arrAnswerHistory.Add(0);
}
arrAnswerHistory.Add(rblAn swer.Selec tedItem.Va lue);
if(intQuestionNo==intTotal Question) {
QuizScreen.Visible = false;
ResultScreen.Visible = true;
ShowResult();
}
QuizScreen.Visible = true;
ResultScreen.Visible = false;
intQuestionNo += 1;
ShowQuestion(intQuestionNo );
}
private void ShowQuestion(int intQuestionNo) {
string strXPath;
int intLoop;
TimeSpan objTimeSpent;
strXPath = "/quiz/mchoice[intQuestion No.ToStrin g()]";
xNodeIterator = xNav.Select(strXPath + "/question");
xNodeIterator.MoveNext();
lblQuestion.Text = intQuestionNo.ToString() + ". " + xNodeIterator.Current.Valu e;
xNodeIterator = xNav.Select(strXPath + "/answer");
rblAnswer.Items.Clear();
intLoop = 0;
while(xNodeIterator.MoveNe xt()){
intLoop += 1;
rblAnswer.Items.Add(new ListItem(xNodeIterator.Cur rent.Value , intLoop.ToString()));
if(xNodeIterator.Current.G etAttribut e("correct ","") == "yes") {
ViewState["CorrectAnswer"] = intLoop;
}
}
lblTotalQuestion.Text = intTotalQuestion.ToString( );
objTimeSpent = DateTime.Now.Subtract((Dat eTime)this .ViewState ["StartTim e"]);
lblTimeSpent.Text = objTimeSpent.Minutes.ToStr ing() + ":" + objTimeSpent.Seconds.ToStr ing();
ViewState["TotalQuestion"] = intTotalQuestion;
ViewState["Score"] = intScore;
ViewState["QuestionNo"] = intQuestionNo;
ViewState["AnswerHistory"] = arrAnswerHistory;
}
private void ShowResult() {
string strResult;
int intLoop;
string strXPath;
TimeSpan objTimeSpent;
objTimeSpent = DateTime.Now.Subtract((Dat eTime)this .ViewState ["StartTim e"]);
strResult = "<center>";
strResult += "<h3>Quiz Result</h3>";
strResult += "<p>Points: " + intScore.ToString() + " of " + intTotalQuestion.ToString( );
strResult += "<p>Your Competency: " + (intScore/intTotalQuestion *100).ToSt ring() + "%%";
strResult += "<p>Time Spent: " + objTimeSpent.Minutes.ToStr ing() + ":" + objTimeSpent.Seconds.ToStr ing();
strResult += "</center>";
strResult += "<h3>Quiz Breakdown:</h3>";
for(intLoop=1;intLoop<=int TotalQuest ion;intLoo p++) {
strXPath = "/quiz/mchoice[" + intLoop.ToString() + "]";
xNodeIterator = xNav.Select(strXPath + "/question");
xNodeIterator.MoveNext();
strResult += "<b>" + intLoop.ToString() + ". " + xNodeIterator.Current.Valu e + "</b><br>";
if((arrAnswerHistory.Count - 1) == 0) {
strResult += "<b>Correct</b><br><br>";
} else {
xNodeIterator = xNav.Select(strXPath + "/answer[" + (arrAnswerHistory.Count - 1).ToString() + "]");
xNodeIterator.MoveNext();
strResult += "<b>You answered:</b> " + xNodeIterator.Current.Valu e + "<br>";
strResult += "<b>Incorrect</b></font><b r><br>";
}
}
lblResult.Text = strResult;
}
}
}
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls
using System.Xml;
using System.Xml.XPath;
namespace Pace
{
/// <summary>
/// Summary description for quiz.
/// </summary>
public class quiz : System.Web.UI.Page{
protected Button btnSubmit;
protected Label lblQuestion;
protected RadioButtonList rblAnswer;
protected Label lblTotalQuestion;
protected Label lblTimeSpent;
protected Label lblResult;
protected Panel QuizScreen;
protected Panel ResultScreen;
XPathNavigator xNav;
XPathDocument xDoc;
XPathNodeIterator xNodeIterator;
System.Collections.ArrayLi
int intTotalQuestion;
int intQuestionNo;
int intScore;
protected override void OnInit(EventArgs e) {
base.OnInit(e);
this.InitializeComponent()
string strXmlDoc = "default.xml";
string strXmlFilePath = Server.MapPath("~/media/qu
XPathDocument xDoc = new XPathDocument(strXmlFilePa
XPathNavigator xNav = xDoc.CreateNavigator();
}
private void InitializeComponent() {
this.btnSubmit.Click += new EventHandler(this.btnSubmi
}
public static void Main() {
}
private void Page_Load(object sender, System.EventArgs e){
this.Main();
if(!Page.IsPostBack){
intTotalQuestion = xNav.Select("/quiz/mchoice
ViewState["StartTime"] = DateTime.Now;
ShowQuestion(intQuestionNo
}
}
private void btnSubmit_Click(object src, EventArgs e) {
intTotalQuestion = (int)this.ViewState["Total
intQuestionNo = (int)this.ViewState["Quest
intScore = (int)this.ViewState["Score
arrAnswerHistory = (System.Collections.ArrayL
if(rblAnswer.SelectedItem.
intScore += 1;
arrAnswerHistory.Add(0);
}
arrAnswerHistory.Add(rblAn
if(intQuestionNo==intTotal
QuizScreen.Visible = false;
ResultScreen.Visible = true;
ShowResult();
}
QuizScreen.Visible = true;
ResultScreen.Visible = false;
intQuestionNo += 1;
ShowQuestion(intQuestionNo
}
private void ShowQuestion(int intQuestionNo) {
string strXPath;
int intLoop;
TimeSpan objTimeSpent;
strXPath = "/quiz/mchoice[intQuestion
xNodeIterator = xNav.Select(strXPath + "/question");
xNodeIterator.MoveNext();
lblQuestion.Text = intQuestionNo.ToString() + ". " + xNodeIterator.Current.Valu
xNodeIterator = xNav.Select(strXPath + "/answer");
rblAnswer.Items.Clear();
intLoop = 0;
while(xNodeIterator.MoveNe
intLoop += 1;
rblAnswer.Items.Add(new ListItem(xNodeIterator.Cur
if(xNodeIterator.Current.G
ViewState["CorrectAnswer"]
}
}
lblTotalQuestion.Text = intTotalQuestion.ToString(
objTimeSpent = DateTime.Now.Subtract((Dat
lblTimeSpent.Text = objTimeSpent.Minutes.ToStr
ViewState["TotalQuestion"]
ViewState["Score"] = intScore;
ViewState["QuestionNo"] = intQuestionNo;
ViewState["AnswerHistory"]
}
private void ShowResult() {
string strResult;
int intLoop;
string strXPath;
TimeSpan objTimeSpent;
objTimeSpent = DateTime.Now.Subtract((Dat
strResult = "<center>";
strResult += "<h3>Quiz Result</h3>";
strResult += "<p>Points: " + intScore.ToString() + " of " + intTotalQuestion.ToString(
strResult += "<p>Your Competency: " + (intScore/intTotalQuestion
strResult += "<p>Time Spent: " + objTimeSpent.Minutes.ToStr
strResult += "</center>";
strResult += "<h3>Quiz Breakdown:</h3>";
for(intLoop=1;intLoop<=int
strXPath = "/quiz/mchoice[" + intLoop.ToString() + "]";
xNodeIterator = xNav.Select(strXPath + "/question");
xNodeIterator.MoveNext();
strResult += "<b>" + intLoop.ToString() + ". " + xNodeIterator.Current.Valu
if((arrAnswerHistory.Count
strResult += "<b>Correct</b><br><br>";
} else {
xNodeIterator = xNav.Select(strXPath + "/answer[" + (arrAnswerHistory.Count - 1).ToString() + "]");
xNodeIterator.MoveNext();
strResult += "<b>You answered:</b> " + xNodeIterator.Current.Valu
strResult += "<b>Incorrect</b></font><b
}
}
lblResult.Text = strResult;
}
}
}
what do you get when you run debug and look at the loading in oninit ?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.