Click here to Skip to main content
Click here to Skip to main content

How to bind an XML file to a GridView

By , 24 Jul 2006
Rate this:
Please Sign up or sign in to vote.

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

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)

About the Author

fstrahberger
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 PinmemberSharon Dymond6-Nov-10 9:49 
QuestionFiltering? PinmemberRonni Marker22-Oct-09 2:22 
Questionhow to bind the xml file exported from tally to gridview? PinmemberShalini_Jha17-Aug-09 19:14 
Questionhow to insert data ? Pinmembersrikanth ravipati10-Sep-08 20:21 
Questionhow to add controls to gridview if grdi contains xml data? Pinmembersrikanth ravipati10-Sep-08 20:13 
GeneralGrid view does not show any data Pinmemberaspnet2.developer21-Nov-07 2:30 
GeneralRe: Grid view does not show any data Pinmemberaspnet2.developer21-Nov-07 2:32 
GeneralThanks PinmemberTheMara17-Aug-07 0:35 
Generaldoesn't work Pinmemberimperialx19-Jun-07 20:42 
GeneralRe: doesn't work Pinmemberfstrahberger19-Jun-07 21:04 
Do you have any error message?
Are there not rows in the GridView?

GeneralRe: doesn't work Pinmemberimperialx20-Jun-07 1:20 
Questionxml PinmemberMember #377932031-Jan-07 20:18 
GeneralEditing the xml contents PinmemberSalam Y. ELIAS19-Aug-06 8:31 
GeneralSimple and to the point PinmemberTinymouse31-Jul-06 19:02 
GeneralRe: Simple and to the point Pinmemberfstrahberger31-Jul-06 21:44 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140415.2 | Last Updated 25 Jul 2006
Article Copyright 2006 by fstrahberger
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid