43157

Question:
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
Answer1: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);
}
Answer2:Try this code instead of your last line of code:
listBox1.DataSource = names;
this.listBox1.DisplayMember = YOURDISPLAYMEMBER;
this.listBox1.ValueMember = YOURVALUEMEMBER;
Answer3:maybe:
listBox1.Items.AddRange(doc.Root.Descendants("Module").Elements("Name").Select(b => b.Value).ToArray());