Click here to Skip to main content
13,253,212 members (50,206 online)
Click here to Skip to main content
Add your own
alternative version


30 bookmarked
Posted 24 Jul 2006

How to bind an XML file to a GridView

, 24 Jul 2006
Rate this:
Please Sign up or sign in to vote.
How to bind an XML file to a GridView.

Sample Image - Bind_XML_file_to_GridView.jpg


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 version="1.0" encoding="utf-8" ?>

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.

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
    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.


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


About the Author

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:

For video tutorials about, ajax, gridviews, ... (in german) visit:

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
Sharon Dymond6-Nov-10 10:49
memberSharon Dymond6-Nov-10 10:49 
QuestionFiltering? Pin
Ronni Marker22-Oct-09 3:22
memberRonni Marker22-Oct-09 3:22 
Questionhow to bind the xml file exported from tally to gridview? Pin
Shalini_Jha17-Aug-09 20:14
memberShalini_Jha17-Aug-09 20:14 
Questionhow to insert data ? Pin
srikanth ravipati10-Sep-08 21:21
membersrikanth ravipati10-Sep-08 21:21 
Questionhow to add controls to gridview if grdi contains xml data? Pin
srikanth ravipati10-Sep-08 21:13
membersrikanth ravipati10-Sep-08 21:13 
GeneralGrid view does not show any data Pin
aspnet2.developer21-Nov-07 3:30
memberaspnet2.developer21-Nov-07 3:30 
GeneralRe: Grid view does not show any data Pin
aspnet2.developer21-Nov-07 3:32
memberaspnet2.developer21-Nov-07 3:32 
GeneralThanks Pin
TheMara17-Aug-07 1:35
memberTheMara17-Aug-07 1:35 
Generaldoesn't work Pin
imperialx19-Jun-07 21:42
memberimperialx19-Jun-07 21:42 
GeneralRe: doesn't work Pin
fstrahberger19-Jun-07 22:04
memberfstrahberger19-Jun-07 22:04 
Do you have any error message?
Are there not rows in the GridView?

GeneralRe: doesn't work Pin
imperialx20-Jun-07 2:20
memberimperialx20-Jun-07 2:20 
Questionxml Pin
Member #377932031-Jan-07 21:18
memberMember #377932031-Jan-07 21:18 
GeneralEditing the xml contents Pin
Salam Y. ELIAS19-Aug-06 9:31
memberSalam Y. ELIAS19-Aug-06 9:31 
GeneralSimple and to the point Pin
Tinymouse31-Jul-06 20:02
memberTinymouse31-Jul-06 20:02 
GeneralRe: Simple and to the point Pin
fstrahberger31-Jul-06 22:44
memberfstrahberger31-Jul-06 22: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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171114.1 | Last Updated 25 Jul 2006
Article Copyright 2006 by fstrahberger
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid