[英]Not showing all data from xml to listbox
I'm trying to load data from the nodes in my xml file to get them to post in a listbox. 我正在尝试从xml文件中的节点加载数据,以使它们发布到列表框中。 Here is what my xml file looks like.
这是我的xml文件的样子。
<?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.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 ? 谁能告诉我我的问题在哪里? thanks.
谢谢。
iterate over your Movie
s not your MovieData
遍历
Movie
而不是MovieData
try
{
XmlDocument doc = new XmlDocument();
doc.Load("XMLFile1.xml");
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.