Jump to content
  • Advertisement
Sign in to follow this  
Kryptus

XML Problem

This topic is 4731 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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);
                }
            }


Share this post


Link to post
Share on other sites
Advertisement
Are you sure that in your "Games.xml", the chapter node has a "name"?

The exception is saying that you don't.

Share this post


Link to post
Share on other sites
Here's the XML source, maybe I've done something wrong?


<?xml version="1.0" encoding="iso-8859-1"?>
<supported>
<games>
<game name="Battlefield 1942" icon="bf1942.ico"></game>
<game name="Battlefield 2" icon="bf2.ico"></game>
</games>
</supported>




[Edited by - Kryptus on August 2, 2005 8:03:41 PM]

Share this post


Link to post
Share on other sites
matibee is correct. to be valid xml you can only have one main node. You havent' closed your node yet as your ending tag doesn't match the beginning tag.

Cheers
Chris

Share this post


Link to post
Share on other sites
The </support> instead of </supported> isn't the problem. It's just a spelling mistake when I typed it here for gamedev.

Share this post


Link to post
Share on other sites
Your XPath query can be changed to simplify this to a single loop, and casting each "game" element as an XmlElement in the foreach loop allows you to call the GetAttribute method to retrieve "name"...

i.e.


XmlDocument xmldoc = new XmlDocument();

xmldoc.Load("C:\\Games.xml");

XmlNodeList games = xmldoc.SelectNodes("//supported/games/game");

foreach (XmlElement game in games)
{
MessageBox.Show( game.GetAttribute("name") );
}






Not necessarily the most defensive way to code that, but does it achieve what you are trying to do?

The reason your code does not work is because the indexer (ie [] operator) for XmlNode only applies to child nodes that are elements, of which your "game" element has none.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!