473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 1778

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: silviu | last post by:
I have the following XML string that I want to parse using the SAX parser. If I remove the portion of the XML string between the <audit> and </audit> tags the SAX is parsing correctly. Otherwise...
0
by: Prakash | last post by:
Hi all, I am trying a parse a xml document containing japanese text by constructing a DOMBuilder object. The document created after parsing is empty. If the xml document does not contain...
6
by: Ulrich Vollenbruch | last post by:
Hi all! since I'am used to work with matlab for a long time and now have to work with c/c++, I have again some problems with the usage of strings, pointers and arrays. So please excuse my basic...
10
by: Bryce Calhoun | last post by:
Hello, First of all, this is a .NET 1.1 component I'm creating. SUMMARY ----------------------- This component that I'm creating is, for all intents and purposes, a document parser (I'm...
4
by: Richard | last post by:
Hi, I like a demo on layers posted at http://www.echoecho.com/csslayers.htm. It displays two text phrases in separate layers visually overlapped, first with one on top and the other beneath,...
1
by: David Hirschfield | last post by:
Anyone out there use simpleparse? If so, I have a problem that I can't seem to solve...I need to be able to parse this line: """Cen2 = Cen(OUT, "Cep", "ies", wh, 544, (wh/ht));""" with this...
1
by: Martin Pöpping | last post by:
Hello, I´ve a problem with parsing a double value from an xml file. My code looks like this: int concept_id; double rank; XmlElement root = documentXMLString.DocumentElement; XmlNodeList...
27
by: comp.lang.tcl | last post by:
My TCL proc, XML_GET_ALL_ELEMENT_ATTRS, is supposed to convert an XML file into a TCL list as follows: attr1 {val1} attr2 {val2} ... attrN {valN} This is the TCL code that does this: set...
12
by: Julian | last post by:
Hi, I am having problems with a function that I have been using in my program to read sentences from a 'command file' and parse them into commands. the surprising thing is that the program works...
7
by: souravmallik | last post by:
Hello, I'm facing a big logical problem while writing a parser in VC++ using C. I have to parse a file in a chunk of bytes in a round robin fashion. Means, when I select a file, the parser...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.