---
title: "Binding XML Data to ListView Control"  
description: "In this article we will discuss how to bind an XML File Data to ListView Control"  
author: "Shankar M"  
published: 2013-04-13  
updated: 2019-09-07  
canonical: https://www.mindstick.com/articles/1251/binding-xml-data-to-listview-control  
category: "c#"  
tags: ["c#"]  
reading_time: 3 minutes  

---

# Binding XML Data to ListView Control

In this article we will discuss how to bind an [XML File](https://www.mindstick.com/articles/75/how-to-read-and-write-xml-file-through-c-sharp) Data to ListView Control

##### Introduction

Here, we will learn how to bind [XML data](https://www.mindstick.com/forum/131/problem-in-showing-the-xml-data-in-well-formed) to ListView control using DataSet Class. Prior Starting,

##### What is XML?

1. XML is acronym [eXtensible](https://www.mindstick.com/blog/59/xaml-extensible-application-markup-language) [Markup Language](https://www.mindstick.com/interview/234/what-is-markup-language).

2. It was primarily designed to carry/transport data whereas HTML is used to display/present data although it is much like HTML.

3. XML document contains [structured](https://yourviews.mindstick.com/view/88498/underbust-corset-styling-guide-for-structured-everyday-looks) information. It is based on tags.(See Employee.xml file)

##### Sample XML file

##### Employee.xml

##### ```
<?xml version="1.0" ?> <EMPLOYEES> <EMP>  <EMPNO>7369</EMPNO>  <ENAME>SMITH</ENAME>  <JOB>CLERK</JOB>  <MGR>7902</MGR>  <HIREDATE>17-DEC-80</HIREDATE>  <SAL>800</SAL>  </EMP> <EMP>  <EMPNO>7499</EMPNO>  <ENAME>ALLEN</ENAME>  <JOB>SALESMAN</JOB>  <MGR>7698</MGR>  <HIREDATE>20-FEB-81</HIREDATE>  <SAL>1600</SAL>  <COMM>300</COMM>  </EMP> <EMP>  <EMPNO>7521</EMPNO>  <ENAME>WARD</ENAME>  <JOB>SALESMAN</JOB>  <MGR>7698</MGR>  <HIREDATE>22-FEB-81</HIREDATE>  <SAL>1250</SAL>  <COMM>500</COMM>  </EMP> <EMP>  <EMPNO>7566</EMPNO>  <ENAME>JONES</ENAME>  <JOB>MANAGER</JOB>  <MGR>7839</MGR>  <HIREDATE>02-APR-81</HIREDATE>  <SAL>2975</SAL>  </EMP> <EMP>  <EMPNO>7654</EMPNO>  <ENAME>MARTIN</ENAME>  <JOB>SALESMAN</JOB>  <MGR>7698</MGR>  <HIREDATE>28-SEP-81</HIREDATE>  <SAL>1250</SAL>  <COMM>1400</COMM>  </EMP>  </EMPLOYEES>
```

#####

##### ListView Control

1. ListView control is used to display list of items. It provides a flexible way to list items displayed in ListView.

2. Each row in the ListView is a ListViewItem. Each column in a ListViewItem is a SubItem.

##### DataSet Class

1. DataSet is a [collection](https://www.mindstick.com/articles/1718/collections-in-java) of DataTables.

2. It is used in combination with [DataAdapter](https://www.mindstick.com/articles/120/dataadapter) class to build and fill each DataTable in a DataSet.

3. DataSet works in [disconnected](https://www.mindstick.com/blog/300962/how-modern-life-disconnected-from-nature) state.

##### Source Code

```
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms; namespace Reading_XML_Data{    public partial class frmEmployee : Form    {        public frmEmployee()        {            InitializeComponent();        }         private void Form1_Load(object sender, EventArgs e)        {            lsview_details.View = View.Details;            lsview_details.GridLines = true;            lsview_details.FullRowSelect = true;            lsview_details.Columns.Add("Employee Id", 100);            lsview_details.Columns.Add("Name", 100);            lsview_details.Columns.Add("Job", 100);            lsview_details.Columns.Add("Reporting", 100);            lsview_details.Columns.Add("Joining Date", 100);            lsview_details.Columns.Add("Salary", 50);            lsview_details.Columns.Add("Commission", 50);         }         private void btn_load_Click(object sender, EventArgs e)        {            DataSet ds = new DataSet();            ds.ReadXml(@"D:\Employee.xml");             ListViewItem item;             foreach (DataRow dr in ds.Tables["EMP"].Rows)            {                item = new ListViewItem(new string[] { dr["EMPNO"].ToString(), dr["ENAME"].ToString(), dr["JOB"].ToString(),                dr["MGR"].ToString(),                dr["HIREDATE"].ToString(),                dr["SAL"].ToString(),                dr["COMM"].ToString()});                lsview_details.Items.Add(item);            }            btn_load.Enabled = false;        }         private void btn_close_Click(object sender, EventArgs e)        {            this.Close();        }    }} 
```

##### Explanation

##### ```
private void Form1_Load(object sender, EventArgs e)        {            lsview_details.View = View.Details;            lsview_details.GridLines = true;            lsview_details.FullRowSelect = true;            lsview_details.Columns.Add("Employee Id", 100);            lsview_details.Columns.Add("Name", 100);            lsview_details.Columns.Add("Job", 100);            lsview_details.Columns.Add("Reporting", 100);            lsview_details.Columns.Add("Joining Date", 100);            lsview_details.Columns.Add("Salary", 50);            lsview_details.Columns.Add("Commission", 50);         }
```

lsview_details.View = View.Details;

The View property defines the way how the items are displayed. The View Enumernation has View.Details, View.LargeIcon, View.List, View.SmallIcon, View.Tile.

lsview_details.GridLines = true;

Indicates that grid line should appear between the [rows and columns](https://answers.mindstick.com/qa/109503/how-to-freeze-rows-and-columns-in-excel).

lsview_details.FullRowSelect = true;

Indicates that selecting a rows selects all its subitems.

lsview_details.Columns.Add("Employee Id", 100);

Defining the column Header with the width of Header

The code Snippet,

```
private void btn_load_Click(object sender, EventArgs e)        {            DataSet ds = new DataSet();            ds.ReadXml(@"D:\Employee.xml");             ListViewItem item;             foreach (DataRow dr in ds.Tables["EMP"].Rows)            {                item = new ListViewItem(new string[] { dr["EMPNO"].ToString(), dr["ENAME"].ToString(), dr["JOB"].ToString(),                dr["MGR"].ToString(),                dr["HIREDATE"].ToString(),                dr["SAL"].ToString(),                dr["COMM"].ToString()});                lsview_details.Items.Add(item);            }            btn_load.Enabled = false;        }
```

\

We create an instance of the DataSet class,

DataSet ds = new DataSet();

And,

ds.ReadXml(@"D:\Employee.xml");

Reads the XML data from the Location Specified, and Load it to the DataSet using ReadXml() Method.

The ReadXml () method takes the parameter as Path of the XML File.

Next, we bind the data from the DataSet to the ListView control

ListViewItem item;

```
foreach (DataRow dr in ds.Tables["EMP"].Rows)            {                item = new ListViewItem(new string[]                             {                               dr["EMPNO"].ToString(),                               dr["ENAME"].ToString(),                               dr["JOB"].ToString(),                               dr["MGR"].ToString(),                               dr["HIREDATE"].ToString(),                               dr["SAL"].ToString(),                               dr["COMM"].ToString()                            }                            );                lsview_details.Items.Add(item);            } 
```

\

Using the Foreach, loop through the data in the DataSet we add the rows to the ListView control using ListViewItem string[] constructor.\

##### ScreenShot

![Binding XML Data to ListView Control](https://www.mindstick.com/mindstickarticle/b065d7e4-e6a5-4d01-b66f-de1400e63916/images/eed5f117-5f48-4d9d-b11a-735fa011c785.png)

##### Article Roundup

In this article we discussed, how to bind the data from XML data to ListView control using the Dataset objects ReadXML()

---

Original Source: https://www.mindstick.com/articles/1251/binding-xml-data-to-listview-control

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
