private void btnCount_Click(object sender, System.EventArgs e) { XmlDocument studentData = null; XmlNodeList studentList = null; 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("//STUDNT"); try { if (studentList != null && studentList.Count > 0) { totalClass = new TotalClass(); foreach (XmlElement student in studentList) { gender = student.GetAttribute("Gender").ToUpper(); switch (gender) { case "F": totalClass.Females++; break; default: case "M": totalClass.Males++; break; } }// end loop } } catch (Exception ex) { Console.WriteLine(ex); } this.lblMales.Text = totalClass.Males.ToString(); this.lblFemale.Text = totalClass.Females.ToString(); this.lblTotal.Text = (totalClass.Females + totalClass.Males).ToString(); } //btnCount_Click
private void btnCount_Click(object sender, System.EventArgs e) { XmlDocument studentData = null; XmlNodeList studentList = null; // TotalClass totalClass = null; // can't be null because students need to be categorized as male or female TotalClass totalClass = new TotalClass(); // create a new Total Class Object 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("//STUDNT"); // changed "//STUDENT" to "//STUDNT to match studentData if (studentList != null && studentList.Count > 0) { foreach (XmlElement student in studentList) { gender = student.GetAttribute("Gender"); switch (gender) { case "F": case "f": // check for lowercase character "f" totalClass.Females++; break; case "M": case "m": // check for lowercase characetr "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(); }//btnCount_Click
private void btnCount_Click(object sender, System.EventArgs e) { XmlDocument studentData = null; XmlNodeList studentList = null; TotalClass totalClass = new TotalClass(); //used to be null //need instance because TotalClass is reference (user-def) type and not //a value type. Will not default to anything. 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("//STUDNT"); //changed to recognize proper label if (studentList != null && studentList.Count > 0) { foreach (XmlElement student in studentList) { gender = student.GetAttribute("Gender"); switch (gender.ToUpper()) //to fix XML case issues (f vs F, m vs M) { 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(); } //btnCount_Click