472,144 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,144 software developers and data experts.

problem with XML parsing

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

Nov 25 '05 #1
5 1739

kotori wrote:
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.

Nov 25 '05 #2
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.

Nov 25 '05 #3

kotori wrote:
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(...)

Nov 25 '05 #4
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.

Nov 25 '05 #5
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

Nov 25 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by silviu | last post: by
6 posts views Thread by Ulrich Vollenbruch | last post: by
4 posts views Thread by Richard | last post: by
1 post views Thread by David Hirschfield | last post: by
1 post views Thread by Martin Pöpping | last post: by
12 posts views Thread by Julian | last post: by
reply views Thread by Saiars | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.