hi, i have the following XML data file:
<?xml version="1.0" encoding="utf-8"?>
<!--This is a creature file generated by Kotori's C# code-->
<!--This file will contain every enemies stats in the RPG-->
<creatures>
<enemy name="Wolf">
<level>1</level>
<health>6</health>
<attack>5</attack>
<defense>3</defense>
<mana>0</mana>
<gold>2</gold>
<exp>2</exp>
</enemy>
<enemy name="Slime">
<level>1</level>
<health>5</health>
<attack>3</attack>
<defense>2</defense>
<mana>0</mana>
<gold>2</gold>
<exp>2</exp>
</enemy>
<enemy name="Hornet">
<level>1</level>
<health>5</health>
<attack>5</attack>
<defense>2</defense>
<mana>0</mana>
<gold>3</gold>
<exp>2</exp>
</enemy>
</creatures>
I am trying to get get a list of all the enemies names to print out in
the console. I'm using the following code:
....
XmlElement root = xmlDoc.DocumentElement;
XmlNodeList nodes = root.SelectNodes("/creatures/enemy");
foreach (XmlNode node in nodes)
{
string name = node["name"].InnerText;
Console.WriteLine("LOADED: \t{0}", name);
}
....
but i get the following error:
An unhandled exception of type 'System.NullReferenceException' occurred
in Xml_Parser.exe
Additional information: Object reference not set to an instance of an
object.
my compiler highlights the "string name = node["name"].InnerText;" line
in yellow, so i suppose that means the compiler (VS.NET 2003) thinks
that is the troublesome line. I was following an example online, so i
have no idea if this is even close to what i need to be doing.
If anyone has an idea about howto properly code what i am trying to do,
please give me the heads up. I've read through Countless articles
about XML already. Thanks in Advance.
~ Kotori