problem with XML parsing

K

kotori

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
 
B

Bruce Wood

kotori said:
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> [snip]
</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

Shouldn't the offending line be:

string name = node.Attributes["name"].InnerText;

?
Of course, you really should test for missing attributes, etc: allow
for the possibility that your XML got corrupted:

Attribute nameAttr = node.Attributes["name"];
if (nameAttr == null)
{
... figure out what to do if there is no "name"
}
else
{
name = nameAttr.InnerText;
}

etc.
 
K

kotori

thanks for the quick reply. I did as u suggested, and believe it or
not i came up with another error:

An unhandled exception of type 'System.FormatException' occurred in
mscorlib.dll

Additional information: Index (zero based) must be greater than or
equal to zero and less than the size of the argument list.
"foreach (XmlNode node in nodes)" is the offending line this time.
 
B

Bruce Wood

kotori said:
thanks for the quick reply. I did as u suggested, and believe it or
not i came up with another error:

An unhandled exception of type 'System.FormatException' occurred in
mscorlib.dll

Additional information: Index (zero based) must be greater than or
equal to zero and less than the size of the argument list.
"foreach (XmlNode node in nodes)" is the offending line this time.

That sounds like a problem with your Console.WriteLine(...)
 
K

kotori

i've tried changing it to just "Console.WriteLine(strName);" but alas:

An unhandled exception of type 'System.NullReferenceException' occurred
in Rpg_Parser.exe

Additional information: Object reference not set to an instance of an
object.
"strName = node.Attributes["name"].InnerText;" seems to be the problem
yet again, i'm thinking that listing XML nodes shouldn't be this hard,
lol.
 
K

kotori

Well i figured it out... i just had to do a little code revision:
....
XmlNodeList nodes = xmlDoc.DocumentElement.SelectNodes(
"/creatures/enemy/name" );
foreach( XmlNode node in nodes )
{
Console.WriteLine( node.InnerText );
}
.....
works Exactly how i wanted it to. thanks for the help Bruce.

~ Kotori
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top