XML and Visual c# problem

Given the following code:

private void button1_Click(object sender, EventArgs e)
{
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();

//load up the xml file
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(people1.InnerText + " " + people2.InnerText);
listBox1.Items.Add("Age: " + people3.InnerText);

System.Xml.XmlNode people4 = peopleitems.Item(1).SelectSingleNode("firstname");
System.Xml.XmlNode people5 = peopleitems.Item(1).SelectSingleNode("lastname");
System.Xml.XmlNode people6=peopleitems.Item(1).SelectSingleNode("age");

add_space();

listBox1.Items.Add(people4.InnerText+" "+people5.InnerText);
listBox1.Items.Add("Age: "+people6.InnerText);

//This is where I get the exception
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");

add_space();

listBox1.Items.Add(people7.InnerText + " " + people8.InnerText);
listBox1.Items.Add("Age: " + " " + people9.InnerText);

and the following XML file:


<?xml version="1.0" encoding="utf-8"?>
-<people>
-<person>
<firstname>Tiffany</firstname>
<lastname>Smith</lastname>
<age>28</age>
</person>
-<person>
<firstname>Anne</firstname>
<lastname>Onymous</lastname>
<age>16</age>
</person>
-<person>
<firstname>Georgina</firstname>
<lastname>Smith</lastname>
<age>34</age>
</person>
</people>

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 ? ;)

[9609 byte] By [MartinHooper] at [2008-2-7]
# 1

Martin -

I am able to run your code (with the listboxes and add_space calls removed) without any exceptions. Given the docuemnt you show at the bottom, the code should be okay.

You can use foreach to simplify this, writing it as:

StreamReader sr = new StreamReader(@"c:\people.xml");

XmlTextReader xr = new XmlTextReader(sr);

XmlDocument peopledoc = new XmlDocument();

//load up the xml file

peopledoc.Load(xr);

XmlNodeList peopleitems = peopledoc.SelectNodes("people/person");

foreach(XmlNode person in peopleitems) {

string firstName = person.SelectSingleNode("firstname").InnerText;

string lastName = person.SelectSingleNode("lastname").InnerText;

string age = person.SelectSingleNode("age").InnerText;

listBox1.Items.Add(firstName + " " + lastName);

listBox1.Items.Add("Age: " + age);

add_space();

}

Thanks,
Luke Hoban
Visual C# IDE Program Manager

LukeHoban at 2007-9-8 > top of Msdn Tech,Visual Studio Express Editions,Visual C# 2005 Express Edition...
# 2
Thanks Luke,

Just tried your supplied code and it still doesn't get the third entry in the xml file...

Is there a bug I wonder...

MartinHooper at 2007-9-8 > top of Msdn Tech,Visual Studio Express Editions,Visual C# 2005 Express Edition...