• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

c# reading from xml file problem

Status
Not open for further replies.
Im trying to load data from the nodes in my xml file to get them to post in a listbox.
Here is what my xml file looks like.

Code:
<?xml version="1.0" encoding="utf-8"?>
<MovieData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Movie>
    <Name>Death Race</Name>
    <Type>Action</Type>
    <Type>Adventure</Type>
    <Rating>R</Rating>
    <Disk>Blu-Ray</Disk>
  </Movie>
  <Movie>
    <Name>Death Race 2</Name>
    <Type>Action</Type>
    <Type>Adventure</Type>
    <Rating>R</Rating>
    <Disk>Blu-Ray</Disk>
  </Movie>
</MovieData>

Here is what i am trying to do.
Code:
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(movieListXML);
                XmlNodeList nodeList = doc.SelectNodes("/MovieData[@*]");
                foreach (XmlNode xn in nodeList)
                {
                    XmlNode movie = xn.SelectSingleNode("Movie");
                    if (movie != null)
                    {
                        movieTypeListBox.Items.Add(movie["Name"].InnerText);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Can anyone tell me where my problem is ? thanks.

The error i keep getting is this.
Data at the root level is invalid. Line 1, position 1.


Note: I do have the required using System directives.

Found the problem.
Code:
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(movieListXML);
                XmlNode node = doc.SelectSingleNode("/MovieData");
                foreach (XmlNode movie in node.SelectNodes("Movie"))
                {
                    if (movie != null)
                    {
                        movieTypeListBox.Items.Add(movie["Name"].InnerText);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
 
Last edited:

peq

peq

Level 6
Joined
May 13, 2007
Messages
171
You can also go to movie-names directly with Xpath.

Code:
foreach (XmlNode name in doc.SelectNodes("/MovieData/Movie/Name")) {
   movieTypeListBox.Items.Add(name.InnerText);
}

Or use "//Movie" to select all Movie elements in the document:

Code:
foreach (XmlNode name in doc.SelectNodes("//Movie/Name")) {
 
You can also go to movie-names directly with Xpath.

Code:
foreach (XmlNode name in doc.SelectNodes("/MovieData/Movie/Name")) {
   movieTypeListBox.Items.Add(name.InnerText);
}

Or use "//Movie" to select all Movie elements in the document:

Code:
foreach (XmlNode name in doc.SelectNodes("//Movie/Name")) {

I saw that option also. Thanks for the info though.
 
Status
Not open for further replies.

Similar threads

Top