Link to home
Start Free TrialLog in
Avatar of Praesidium
PraesidiumFlag for United States of America

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.ArrayList 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/quizzes/default.xml" );
                        XPathDocument xDoc = new XPathDocument(strXmlFilePath);
                        XPathNavigator xNav = xDoc.CreateNavigator();
                  }

                  private void InitializeComponent() {
                        this.btnSubmit.Click += new EventHandler(this.btnSubmit_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["TotalQuestion"];
                        intQuestionNo = (int)this.ViewState["QuestionNo"];
                        intScore = (int)this.ViewState["Score"];
                        arrAnswerHistory = (System.Collections.ArrayList)this.ViewState["AnswerHistory"];

                        if(rblAnswer.SelectedItem.Value == (string)this.ViewState["CorrectAnswer"]) {
                              intScore += 1;
                              arrAnswerHistory.Add(0);
                        }
                  
                        arrAnswerHistory.Add(rblAnswer.SelectedItem.Value);

                        if(intQuestionNo==intTotalQuestion) {

                              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[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.ToString()));

                              if(xNodeIterator.Current.GetAttribute("correct","") == "yes") {
                                    ViewState["CorrectAnswer"] = intLoop;
                              }
                        }
                        lblTotalQuestion.Text = intTotalQuestion.ToString();

                        objTimeSpent = DateTime.Now.Subtract((DateTime)this.ViewState["StartTime"]);
                        lblTimeSpent.Text = objTimeSpent.Minutes.ToString() + ":" + objTimeSpent.Seconds.ToString();

                        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((DateTime)this.ViewState["StartTime"]);

                        strResult  = "<center>";
                        strResult += "<h3>Quiz Result</h3>";
                        strResult += "<p>Points: " + intScore.ToString() + " of " + intTotalQuestion.ToString();
                        strResult += "<p>Your Competency: " + (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.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.Value + "<br>";
                                    strResult += "<b>Incorrect</b></font><br><br>";
                              }
                        }

                        lblResult.Text = strResult;
                  }
            }
}
Avatar of gregoryyoung
gregoryyoung
Flag of Canada image

what do you get when you run debug and look at the loading in oninit ?
ASKER CERTIFIED SOLUTION
Avatar of Snarf0001
Snarf0001
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial