Binding XML file to a ListBox Using C#

Submitted by: 
Visitors have accessed this post 234 times.

Now, here’s another tutorial that you’ll surely want to learn. By this time, I’m going to teach you how to bind an XML file to a listbox using C#. By using this method you will be able to control the connection between XML file and the listbox. It also has the ability to view the content of XML file in the listbox.

Creating XML FIle

Go to solution explorer and right click the file name. After that select “Add” and hit “New Item”.
Choose the “XML File” then click “Add”.
Add data to the XML File.

  2. <NewDataSet>
  3. <Table>
  4. <abbr>BEF</abbr>
  5. <Description>Belgium Francs</Description>
  6. </Table>
  7. <Table>
  8. <abbr>DEM</abbr>
  9. <Description>Germany Deutsche Marks</Description>
  10. </Table>
  11. <Table>
  12. <abbr>ESP</abbr>
  13. <Description>Spain Pesetas</Description>
  14. </Table>
  15. <Table>
  16. <abbr>FRF</abbr>
  17. <Description>France Francs</Description>
  18. </Table>
  19. </NewDataSet>

Creating Application

Step 1

Open Microsoft Visual Studio 2015 and create a new windows form application for c#.

Step 2

Add a ListBox inside the Form just like shown below.

Step 3

Put the XML File in the resources folder by dragging it.

Step 4

Press F7 to open the code editor. In the code editor, create a method to retrieve the data in the ListBox.

  2. private void load_Countries(ListBox lstbox)
  3. {
  4. DataSet dsCountries = new DataSet();
  5. dsCountries.ReadXml(new System.IO.StringReader(Properties.Resources.XMLFile1));
  7. lstbox.DataSource = dsCountries.Tables[0];
  8. lstbox.DisplayMember = "Description";
  9. lstbox.ValueMember = "abbr";
  10. }

Step 5

Write the following code to execute the method that you have created in the first load of the form.

  2. private void Form1_Load(object sender, EventArgs e)
  3. {
  4. load_Countries(listBox1);
  5. }

The complete source code is included. You can download it and run it on your computer.
For any questions about this article. You can contact me @
Email – [email protected]
Mobile No. – 09305235027 – TNT
Or feel free to comment below.

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.