Binding XML Data to ListView Control - DataSet Approach

In this article we will discuss how to bind an XML File Data to a ListView Control.

Introduction

Here, we will learn how to bind XML data to a ListView control using the DataSet Class. Prior starting, let's see what XML is.

What is XML?

  1. XML is the acronym for eXtensible 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 documents contain structured 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. A ListView control is used to display a list of items. It provides a flexible way to list the items displayed in the ListView.
  2. Each row in the ListView is a ListViewItem. Each column in a ListViewItem is a SubItem.

DataSet Class

  1. A DataSet is a collection of DataTables.
  2. It is used in combination with the DataAdapter class to build and fill each DataTable in a DataSet.
  3. The DataSet works in the disconnected 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 how to display the items. The View Enumernation has View.Details, View.LargeIcon, View.List, View.SmallIcon, View.Tile.

 

lsview_details.GridLines = true;

 

     Indicates that a grid line should appear between the rows and columns.

 

lsview_details.FullRowSelect = true;

 

     Indicates that selecting a row selects all its subitems.

 

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

 

      Defining the column Header with the width of the 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 as in the following:

                  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 the ReadXml() Method.

The ReadXml () method takes the path of the XML file as the parameter.

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 Foreach, we loop through the data in the DataSet to add the rows to the ListView control using the ListViewItem string[] constructor.

Output.jpg

Article Roundup

In this article we discussed, how to bind data from XML data to a ListView control using the Dataset objects ReadXML().