Question

I'm 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.

<?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.

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

Updated code still has problem. It only shows one movie name instead of all of the movie names.

try
    {
        XmlDocument doc = new XmlDocument();
        doc.Load(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 ?

Another question: Can someone show me how to organize the data alphabetically by the name of the Movie ?

Was it helpful?

Solution 2

My psychic debugging powers tell me that movieListXML might be a filename in which case you want to call

//movieListXML = @"c:\xmlFile.xml";
doc.Load(movieListXML);

instead of

//movieListXML = @"<MovieData><Movie></Movie></MovieData>";
doc.LoadXml(movieListXML);

which takes the actual XML in as a string

OTHER TIPS

check your xml file - it's likely got a Bite order marker in it. open the file in a hex editor and delete the non printing characters from the start of the file.

As your xml looks fine - particularly the xml declaration - I'm pretty sure this will be your problem.

depending on the character encoding used when the file was created it'll be somehting like : 0xFEFF (if its utf-8 - think that ones little endian)

Here's how to get all the movie names out :

           XmlNodeList nodeList = doc.SelectNodes("/MovieData/Movie");
            foreach (XmlNode xn in nodeList)
            {
                    Console.WriteLine(xn["Name"].InnerText);
            }

if you want to sort them too then its probably easier to XDocument.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top