How Populate Data in a ComboBox Using XML File And C#

Submitted by: 
Language: 
Visitors have accessed this post 117 times.

In this tutorial, I will teach you how to populate data in a combobox using XML File and C#. This method has the ability to display the data from the XML file to a combobox. Hope this tutorial will help you with your current problem in programming. Just follow the instructions below.

Creating XML File

Go to the solution explorer, right click the resources file and add new item.
ps1
Create an XML File named “country”.
ps2
Write the following codes for adding the content in the XML FIle.

  1.  
  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#.
ps1

Step 2

Do the form just like shown below.
ps4

Step 3

Press F7 to open the code editor. In the code editor, add a namespace to access the Resources File .

  1.  
  2. using LoadDataXML.Properties;

Step 4

Create a method for filling the xml data inside the combobox.

  1.  
  2. private void LoadCountry(ComboBox cbo)
  3. {
  4. DataSet dsCountries = new DataSet();
  5. dsCountries.ReadXml(new System.IO.StringReader(Properties.Resources.country));
  6.  
  7. cbo.DataSource = dsCountries.Tables[0];
  8. cbo.DisplayMember = "Description";
  9. cbo.ValueMember = "abbr";
  10. }
  11.  

Step 5

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

  1.  
  2. private void Form1_Load(object sender, EventArgs e)
  3. {
  4. LoadCountry(comboBox1);
  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.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.