How to Load an XML File into a DataGridView
In ADO.NET, the DataSet class implements methods and properties to work with XML documents, including reading XML, loading it into a DataSet and writing a DataSet back to an XML file.
The ReadXml method reads an XML file and loads it into a DataSet. ReadXml is an overloaded method; you can use it to read a data stream, TextReader, XmlReader, or an XML file and store the data into a DataSet object, that can later be used to display the data in a tabular format.
The following code snippet loads a Books.xml file into a DataSet. Here the file C:\Books\Books.xml" is the XML file that you want to display in a DataGridView control.
- DataSet dataSet = new DataSet();
- dataSet.ReadXml(@"C:\Books\Books.xml");
The following code snippet binds the default DataTable with a DataGridView control.
- dataGridView1.DataSource = dataSet.Tables[0];
Sample
- Download the attached file and copy it to your local machine in some folder and change the full path of the XML file passed in the ReadXml method.
- Create a Windows Forms application.
- Add a DataGridView Control to the Form.
- Add a Button control, double-click on it and add the following code to the button click event handler.
- DataSet dataSet = new DataSet();
- dataSet.ReadXml(@"C:\Books\Books.xml");
- dataGridView1.DataSource = dataSet.Tables[0];
The output will be a DataGridView displaying the contents of an XML file.


Priyanka KalePosted Feb 19, 2020, 11:48 PM
Hello Mahesh, can you please help me with to know how to filter this record based on publisher?
Priyanka KalePosted Feb 19, 2020, 11:47 PM
Hello Mahesh,
Juan OrtizPosted Jan 18, 2019, 11:04 AM
How I can show child tables?
KingPosted Nov 13, 2016, 1:14 PM
Book.xml contains author information that is not displayed in the DataGridView. How do you display the first names and last names?
Aggelos TzitzifasPosted Oct 18, 2013, 11:50 AM
i've made a project which introduces XML to a treeview. The properties of the notes are almost the same in everything. I would like to correct their index throught XML in order to use them in the code, I would like to be able to use note tree if i should chose it. (treeViewMenu.SelectedNode.Index) Also, i should inform you that this is not from a data base. Thank you for your time, Aggelos Tzitzifas