In this article I will explain with an example, how to read, parse and import XML file in Windows Forms DataGridView using C# and VB.Net.
The XML file will be read and parsed using a DataSet and all the records from the XML file will be imported into a DataTable within the DataSet.
Finally the DataSet will be used to populate and display the data from XML file in Windows Forms DataGridView.
 
 
The XML file
The following XML file will be used to populate the DataGridView.
<?xml version="1.0" standalone="yes"?>
<Customers>
 <Customer>
    <Id>1</Id>
    <Name>John Hammond</Name>
    <Country>United States</Country>
 </Customer>
 <Customer>
    <Id>2</Id>
    <Name>Mudassar Khan</Name>
    <Country>India</Country>
 </Customer>
 <Customer>
    <Id>3</Id>
    <Name>Suzanne Mathews</Name>
    <Country>France</Country>
 </Customer>
 <Customer>
    <Id>4</Id>
    <Name>Robert Schidner</Name>
    <Country>Russia</Country>
 </Customer>
</Customers>
 
 
Form Controls
The following Form consists of a DataGridView and a Button to the Windows Form.
Read, Parse and Import XML file in Windows DataGridView using C# and VB.Net
 
 
Namespaces
You will need to import the following namespace.
C#
using System.Data;
 
VB.Net
Imports System.Data
 
 
Populating DataGridView from XML file
Inside the Form Load event, first the XML file is selected using the OpenFileDialog control. Then using the path, the XML file is read into the DataSet using the ReadXml method.
Finally the DataSet is used to populate the DataGridView control.
C#
private void btnSelect_Click(object sender, EventArgs e)
{
    OpenFileDialog openFileDialog = new OpenFileDialog();
    if (openFileDialog.ShowDialog() == DialogResult.OK)
    {
        string xmlFilePath = openFileDialog.FileName;
        using (DataSet ds = new DataSet())
        {
            ds.ReadXml(xmlFilePath);
            dataGridView1.DataSource = ds.Tables[0];
        }
    }
}
 
VB.Net
Private Sub btnSelect_Click(sender As System.Object, e As System.EventArgs) Handles btnSelect.Click
    Dim openFileDialog As OpenFileDialog = New OpenFileDialog()
    If openFileDialog.ShowDialog() = DialogResult.OK Then
        Dim xmlFilePath As String = openFileDialog.FileName
        Using ds As DataSet = New DataSet()
            ds.ReadXml(xmlFilePath)
            dataGridView1.DataSource = ds.Tables(0)
        End Using
    End If
End Sub
 
 
Screenshot
Read, Parse and Import XML file in Windows DataGridView using C# and VB.Net
 
 
Downloads