Below is my asp.net code which should fetch the correct answer from the xml document.
how can i store the xml attribute (i.e to an object or string)?
it is throwing an error object reference not set to the instance of the object
could any one help me
<?xml version="1.0" encoding="utf-8" ?>
<quiz>
<mchoice>
<question>Which of the following characteristics distinguishes man from the other animals </question>
<answer>Ability to stand erect </answer>
<answer>Ability to adapt to the environment </answer>
<answer correct="yes">Ability to make tools</answer>
<answer>Ability to live in a group</answer>
</mchoice>
<mchoice>
<question>There is freedom of association between members of all strata including intermarriage
and equalitarian social relations in </question>
<answer correct="yes">open-class </answer>
<answer>open ethnic</answer>
<answer>closed caste</answer>
<answer>closed estate</answer>
</mchoice>
<mchoice>
<question>Who among the following emphasized the role of Charismatic Leadership in social change</question>
<answer>Sorokin</answer>
<answe>Pareto</answe>
<answer correct="yes">Max Weber </answer>
<answer>Rajani Kothari</answer>
</mchoice>
<mchoice>
<question>Opposition to which one of the following was the primary goal of the Kheda Movement of Gujarat </question>
<answer correct="yes">Taxes</answer>
<answer>Sudden price rise </answer>
<answer>Money lending class</answer>
<answer>Unpaid forced labour</answer>
</mchoice>
</quiz>
namespace quiz
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.Label Label3;
protected System.Web.UI.WebControls.RadioButtonList Rdlst;
protected System.Web.UI.WebControls.Button BtnSbt;
string StartTime;
XmlDocument xmldoc=new XmlDocument();
int totalquestion;
int questionno=1;
int score=0;
int[] answerarr=new int[10];
//XPathNodeIterator xpathnodeit=new XPathNodeIterator();
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
Label1.Text="hello";
xmldoc.Load(Server.MapPath("quiz.xml"));
if (Page.IsPostBack!=true)
{
totalquestion=xmldoc.SelectNodes("/quiz/mchoice").Count;
Label1.Text=System.Convert.ToString(totalquestion) ;
ViewState["StartTime"]=DateTime.Now;
ShowQuestion(questionno);
ViewState["TotalQUestion"]=totalquestion;
}
}
public void ShowQuestion(int intquestionno)
{
XmlNodeList xmlnodelist;
Object xmlattr=new object();
string str=null;
int i;
string strpath;
TimeSpan timespent;
Rdlst.Items.Clear();
ViewState["StartTime"]=DateTime.Now;
strpath="/quiz/mchoice["+intquestionno.ToString()+"]";
Label1.Text=questionno.ToString() + "." + xmldoc.SelectSingleNode(strpath+"/question").InnerXml;
xmlnodelist=xmldoc.SelectNodes(strpath+"/answer");
for( i = 0;i<=xmlnodelist.Count-1;i++)
{
Rdlst.Items.Add(new ListItem(xmlnodelist.Item(i).InnerText));
}
ViewState["QuestionNo"]=intquestionno;
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Rdlst.SelectedIndexChanged += new System.EventHandler(this.Rdlst_SelectedIndexChange d);
this.BtnSbt.Click += new System.EventHandler(this.BtnSbt_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Rdlst_SelectedIndexChanged(object sender, System.EventArgs e)
{
}
private void BtnSbt_Click(object sender, System.EventArgs e)
{
questionno=(int)ViewState["QuestionNo"];
totalquestion=(int)ViewState["TotalQUestion"];
questionno+=1;
if(questionno>totalquestion)
{
Response.Redirect("result.aspx");
}
else
ShowQuestion(questionno);
}
}
}