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