XmlDocument studentData;
XmlNodeList studentList;
TotalClass totalClass = null;
string gender = "";
studentData = new XmlDocument();
studentData.LoadXml("<root>"
+ "<STUDNT ID=\"7\" Gender=\"M\"></STUDNT>"
+ "<STUDNT ID=\"16\" Gender=\"F\"></STUDNT>"
+ "<STUDNT ID=\"22\" Gender=\"F\"></STUDNT>"
+ "<STUDNT ID=\"25\" Gender=\"M\"></STUDNT>"
+ "<STUDNT ID=\"27\" Gender=\"F\"></STUDNT>"
+ "<STUDNT ID=\"32\" Gender=\"M\"></STUDNT>"
+ "<STUDNT ID=\"35\" Gender=\"f\"></STUDNT>"
+ "<STUDNT ID=\"45\" Gender=\"M\"></STUDNT>"
+ "<STUDNT ID=\"4423453244\" Gender=\"F\"></STUDNT>"
+ "<STUDNT ID=\"44344\" Gender=\"F\"></STUDNT>"
+ "</root>");
studentList = studentData.SelectNodes("//STUDENT");
if(studentList != null && studentList.Count > 0)
{
foreach(XmlElement student in studentList)
{
gender = student.GetAttribute("Gender");
switch(gender)
{
case "F":
totalClass.Females++;
break;
default:
case "M":
totalClass.Males++;
break;
}
}// end loop
}
this.lblMales.Text = totalClass.Males.ToString();
this.lblFemale.Text = totalClass.Females.ToString();
this.lblTotal.Text = (totalClass.Females + totalClass.Males).ToString();
私はこのプロゴラムにいくつかの助けが必要です。 私はこのプログラムをデバッグするように割り当てられましたが、解決する方法を見つけることができませんでした。カウントボタンをクリックすると、NullReferenceException
が表示されます。xmlファイルをテキストボックスコントロールに表示
[NullReferenceExceptionとは何ですか?また修正するにはどうすればいいですか?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix -it) – mybirthname