Click here to Skip to main content
15,881,852 members
Articles / Programming Languages / XML

How to bind an XML file to a GridView

Rate me:
Please Sign up or sign in to vote.
3.71/5 (13 votes)
24 Jul 2006CPOL 118.6K   689   29   15
How to bind an XML file to a GridView.

Sample Image - Bind_XML_file_to_GridView.jpg

Introduction

Sometimes you want to use an XML file rather than a database to store your information. Maybe you don't have a database at your hosting account or you have mostly static data. This article explains how to bind an XML file to a GridView in four lines of code.

The XML File

First, create a new XML file and populate it with some data. Here is an example:

XML
<?xml version="1.0" encoding="utf-8" ?>
<Employee>
  <em>
    <id>1</id>
    <name>Florian</name>
    <phone>123</phone>
  </em>
  <em>
    <id>2</id>
    <name>Andreas</name>
    <phone>234</phone>
  </em>
  <em>
    <id>3</id>
    <name>Martin</name>
    <phone>345</phone>
  </em>
</Employee>

Bind the XML File to a GridView

Drag a GridView control on to your web form. Next, you need some code to bind the data from the XML file to your GridView.

VB
Imports System.Data
Partial Class _Default
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, _
              ByVal e As System.EventArgs) Handles Me.Load
        Dim oDs As New DataSet
        oDs.ReadXml(Request.PhysicalApplicationPath + "XMLFile.xml")
        GridView1.DataSource = oDs
        GridView1.DataBind()
    End Sub
End Class

In the Page_Load event, we create a new DataSet and use the method ReadXml to get the data from the XML file. Then we bind the DataSet to the GridView.

Points of Interest

In order to avoid "hard coding" the path to the XML file , I use Request.PhysicalApplicationPath to specify the location of the file.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Germany Germany
Florian works as consultant for change- and configuration management for about 7 years. In this environment he is often forced to work with unix, perl and shell scripts.

For more information about change- and configuration management (espacially Serena Dimensions) visit: www.venco.de

For video tutorials about asp.net, ajax, gridviews, ... (in german) visit: www.siore.com

Comments and Discussions

 
GeneralMy vote of 5 Pin
Sharon Dymond6-Nov-10 9:49
Sharon Dymond6-Nov-10 9:49 
QuestionFiltering? Pin
Ronni Marker22-Oct-09 2:22
Ronni Marker22-Oct-09 2:22 
Questionhow to bind the xml file exported from tally to gridview? Pin
Shalini_Jha17-Aug-09 19:14
Shalini_Jha17-Aug-09 19:14 
Questionhow to insert data ? Pin
srikanth ravipati10-Sep-08 20:21
srikanth ravipati10-Sep-08 20:21 
Questionhow to add controls to gridview if grdi contains xml data? Pin
srikanth ravipati10-Sep-08 20:13
srikanth ravipati10-Sep-08 20:13 
GeneralGrid view does not show any data Pin
aspnet2.developer21-Nov-07 2:30
aspnet2.developer21-Nov-07 2:30 
GeneralRe: Grid view does not show any data Pin
aspnet2.developer21-Nov-07 2:32
aspnet2.developer21-Nov-07 2:32 
GeneralThanks Pin
TheMara17-Aug-07 0:35
TheMara17-Aug-07 0:35 
Generaldoesn't work Pin
imperialx19-Jun-07 20:42
imperialx19-Jun-07 20:42 
GeneralRe: doesn't work Pin
fstrahberger19-Jun-07 21:04
fstrahberger19-Jun-07 21:04 
GeneralRe: doesn't work Pin
imperialx20-Jun-07 1:20
imperialx20-Jun-07 1:20 
Questionxml Pin
Member 377932031-Jan-07 20:18
Member 377932031-Jan-07 20:18 
GeneralEditing the xml contents Pin
Salam Y. ELIAS19-Aug-06 8:31
professionalSalam Y. ELIAS19-Aug-06 8:31 
GeneralSimple and to the point Pin
The Head Geek31-Jul-06 19:02
The Head Geek31-Jul-06 19:02 
GeneralRe: Simple and to the point Pin
fstrahberger31-Jul-06 21:44
fstrahberger31-Jul-06 21:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.