XML and Visual c# problem
private void button1_Click(object sender, EventArgs e) //load up the xml file listBox1.Items.Add(people1.InnerText + " " + people2.InnerText); System.Xml.XmlNode people4 = peopleitems.Item(1).SelectSingleNode("firstname"); add_space(); listBox1.Items.Add(people4.InnerText+" "+people5.InnerText); //This is where I get the exception add_space(); listBox1.Items.Add(people7.InnerText + " " + people8.InnerText);
{
System.IO.StreamReader sr = new System.IO.StreamReader(@"people.xml");
System.Xml.XmlTextReader xr = new System.Xml.XmlTextReader(sr);
System.Xml.XmlDocument peopledoc = new System.Xml.XmlDocument();
peopledoc.Load(xr);
System.Xml.XmlNodeList peopleitems = peopledoc.SelectNodes("people/person");
System.Xml.XmlNode people1 = peopleitems.Item(0).SelectSingleNode("firstname");
System.Xml.XmlNode people2 = peopleitems.Item(0).SelectSingleNode("lastname");
System.Xml.XmlNode people3 = peopleitems.Item(0).SelectSingleNode("age");
listBox1.Items.Add("Age: " + people3.InnerText);
System.Xml.XmlNode people5 = peopleitems.Item(1).SelectSingleNode("lastname");
System.Xml.XmlNode people6=peopleitems.Item(1).SelectSingleNode("age");
listBox1.Items.Add("Age: "+people6.InnerText);
System.Xml.XmlNode people7 = peopleitems.Item(2).SelectSingleNode("firstname");
System.Xml.XmlNode people8 = peopleitems.Item(2).SelectSingleNode("lastname");
System.Xml.XmlNode people9 = peopleitems.Item(2).SelectSingleNode("age");
listBox1.Items.Add("Age: " + " " + people9.InnerText);
and the following XML file:
<?xml version="1.0" encoding="utf-8"?>
Why do I get a Null reference exception when trying to read the third entry (creating the people 7,8,9 variables)?
Also is there a way of using a foreach loop to make the code a lot smaller ? ;)

