0

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

3
  • variable names is array or not...! Commented Mar 31, 2012 at 10:48
  • Is it a forms app, web forms, or what? Commented Mar 31, 2012 at 10:51
  • you can use addrange function only with array Commented Mar 31, 2012 at 10:51

3 Answers 3

2

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);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks dude that works nicely, I've substituted that for my XmlTextReader code which populated the listbox instead. Thank you
1

Try this code instead of your last line of code:

listBox1.DataSource = names;
this.listBox1.DisplayMember = YOURDISPLAYMEMBER;
this.listBox1.ValueMember = YOURVALUEMEMBER;

Comments

0

maybe:

listBox1.Items.AddRange(doc.Root.Descendants("Module").Elements("Name").Select(b => b.Value).ToArray());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.