Home > DeveloperSection > Articles > Binding XML Data to ListView Control

Binding XML Data to ListView Control


C# C# 
Ratings:
0 Comment(s)
 5887  View(s)
Rate this:

Binding XML Data to ListView Control

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

Introduction

     Here, we will learn how to bind XML data to ListView control using DataSet Class. Prior Starting,

What is XML?

1.       XML is acronym 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 document contains 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.       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 of DataTables.

2.       It is used in combination with DataAdapter class to build and fill each DataTable in a DataSet.

3.       DataSet works in 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 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.

 

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

Article Roundup

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


Don't want to miss updates? Please click the below button!

Follow MindStick