In this article I will explain with an example, how to bind (populate) ComboBox with XML in Windows Forms (WinForms) Application using C# and VB.Net.
 
 
XML File
Following is the XML consisting of records of Customers.
<?xml version="1.0" standalone="yes"?>
<Customers>
    <Customer>
        <CustomerId>1</CustomerId>
        <Name>John Hammond</Name>
        <Country>United States</Country>
    </Customer>
    <Customer>
        <CustomerId>2</CustomerId>
        <Name>Mudassar Khan</Name>
        <Country>India</Country>
    </Customer>
    <Customer>
        <CustomerId>3</CustomerId>
        <Name>Suzanne Mathews</Name>
        <Country>France</Country>
    </Customer>
    <Customer>
        <CustomerId>4</CustomerId>
        <Name>Robert Schidner</Name>
        <Country>Russia</Country>
    </Customer>
</Customers>
 
 
Form Design
The following Form consists of a ComboBox control and a Button. The Button has been assigned a Click event handler.
Populate ComboBox with XML in Windows Forms using C# and VB.Net
 
 
Namespaces
You will need to import the following namespace.
C#
using System.IO;
 
VB.Net
Imports System.IO
 
 
Bind (Populate) ComboBox from XML data
Inside the Form Load event handler, the contents of the XML file are read into the DataSet using the ReadXml function which accepts the path of the XML file.
Then, the records are copied into the DataTable and a new row is inserted in First position which will act as the Default (Blank) item and the DataTable is set as DataSource for the ComboBox.
Finally, the DisplayMember and ValueMember are set with Name and CustomerId columns respectively.
C#
private void Form1_Load(object sender, EventArgs e)
{
    using (DataSet ds = new DataSet())
    {
        ds.ReadXml(@"E:\Files\Customers.xml");
 
        //Populate the DataTable.
        DataTable dt = ds.Tables["Customer"];
 
        //Insert the Default Item to DataTable.
        DataRow row = dt.NewRow();
        row[0] = 0;
        row[1] = "Please select";
        dt.Rows.InsertAt(row, 0);
 
        //Assign DataTable as DataSource.
        cbCustomers.DataSource = dt;
        cbCustomers.DisplayMember = "Name";
        cbCustomers.ValueMember = "CustomerId";
    }
}
 
VB.Net
Private Sub Form1_Load(sender As Object, e AsEventArgs) Handles MyBase.Load
    Using ds As DataSet = New DataSet()
        ds.ReadXml("E:\Files\Customers.xml")
 
        'Populate the DataTable.
        Dim dt As DataTable = ds.Tables("Customer")
 
        'Insert the Default Item to DataTable.
        Dim row As DataRow = dt.NewRow()
        row(0) = 0
        row(1) = "Please select"
        dt.Rows.InsertAt(row, 0)
 
        'Assign DataTable as DataSource.
        cbCustomers.DataSource = dt
        cbCustomers.DisplayMember = "Name"
        cbCustomers.ValueMember = "CustomerId"
    End Using
End Sub
 
 
Fetching the selected Text and Value of ComboBox
When the Submit Button is clicked, the selected Text and Value of the ComboBox is fetched and displayed using MessageBox.
C#
private void btnSubmit_Click(object sender, EventArgs e)
{
    string message = "Name: " + cbCustomers.Text;
    message += Environment.NewLine;
    message += "CustomerId: " + cbCustomers.SelectedValue;
    MessageBox.Show(message);
}
 
VB.Net
Private Sub btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSubmit.Click
    Dim message As String = "Name: " & cbCustomers.Text
    message += Environment.NewLine
    message += "CustomerId: " & cbCustomers.SelectedValue
    MessageBox.Show(message)
End Sub
 
 
Screenshot
Populate ComboBox with XML in Windows Forms using C# and VB.Net
 
 
Downloads