问题
I don't know how to convert the type for name so that each element in it can be added to my ListBox. If someone could help that would be much appreciated.
XDocument doc = XDocument.Load(workingDir + @"\Moduleslist.xml");
var names = doc.Root.Descendants("Module").Elements("Name").Select(b => b.Value);
listBox1.Items.AddRange(names);
I'm getting an error on AddRange(names) saying invalid arguments
回答1:
names is IEnumerable<String>
and listBox.Items.AddRange
is expecting an object array and there is no implicit cast between them.
A quick solution would be to:
listBox1.Items.AddRange(names.ToArray());
or
foreach (var item in names)
{
listBox1.Items.Add(item);
}
回答2:
Try this code instead of your last line of code:
listBox1.DataSource = names;
this.listBox1.DisplayMember = YOURDISPLAYMEMBER;
this.listBox1.ValueMember = YOURVALUEMEMBER;
回答3:
maybe:
listBox1.Items.AddRange(doc.Root.Descendants("Module").Elements("Name").Select(b => b.Value).ToArray());
来源:https://stackoverflow.com/questions/9955199/adding-xml-elements-to-listbox