Sign in to follow this  

XML Problem

This topic is 4520 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
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
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

This topic is 4520 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this