43157

Adding XML elements to ListBox

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());

Recommend

  • How do you move the cursor to the next element in a contenteditable div in Chrome/Safari?
  • ShiftPageNumbers with iTextSharp
  • Can't edit my datagridview after binding to XML file via linq
  • What is a good way to read this XML?
  • Powershell: Creating Custom DataGridView
  • how to extract text from a html element by id and assign to a php variable?
  • Custom property: gotta be something obvious I'm doing wrong
  • XML delete node according to timestamp C#
  • ListItem.Attributes.Add not working
  • JBoss External Properties Files in Classpath
  • htaccess add www if not subdomain, if subdomain remove www
  • Installed module is empty
  • How do I exclude a dependency in provided scope when running in Maven test scope?
  • Is there a perl module to validate passwords stored in “{crypt}hashedpassword” “{ssha}hashedpassword
  • Meteor helpers not available in Angular template
  • Jenkins: How To Build multiple projects from a TFS repository?
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • Why doesn't :active or :focus work on text links in webkit? (safari & chrome)
  • Can a Chrome extension content script make an jQuery AJAX request for an html file that is itself a
  • When should I choose bucket sort over other sorting algorithms?
  • ORA-29908: missing primary invocation for ancillary operator
  • How to get next/previous record number?
  • php design question - will a Helper help here?
  • Unanticipated behavior
  • VB.net deserialize, JSON Conversion from type 'Dictionary(Of String,Object)' to type '
  • How to delete a row from a dynamic generate table using jquery?
  • Why can't I rebase on to an ancestor of source changesets if on a different branch?
  • using HTMLImports.whenReady not working in chrome
  • Hits per day in Google Big Query
  • How do you join a server to an Active Directory (domain)?
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • How does Linux kernel interrupt the application?
  • Linking SubReports Without LinkChild/LinkMaster
  • Authorize attributes not working in MVC 4
  • EntityFramework adding new object to nested object collection
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • How can i traverse a binary tree from right to left in java?
  • Unable to use reactive element in my shiny app
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How do I use LINQ to get all the Items that have a particular SubItem?