Original Post
I am programming with XML in C# with the code below. The problem is I get an "Object reference not set to an instance of an object" error on the MessageBox.Show(chapterNode["name"].Value); line. Why is this? I've been trying to figure it out, but I can't. I can't use the new keyword on the object can I? How can I fix it?
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load("C:\\Games.xml");
XmlNodeList categories = xmldoc.SelectNodes("supported/games");
// Iterate through all the categories
foreach (XmlNode categoryNode in categories)
{
// Get all chapters in the current category.
XmlNodeList chapters = categoryNode.SelectNodes("game");
// Loop through all chapters in the current category and add to the string.
foreach (XmlNode chapterNode in chapters)
{
MessageBox.Show(chapterNode["name"].Value);
}
}