Click here to Skip to main content
Licence CPOL
First Posted 24 Jul 2006
Views 73,951
Downloads 238
Bookmarked 27 times

How to bind an XML file to a GridView

By | 24 Jul 2006 | Article
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 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

Member

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

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 PinmemberSharon Dymond9:49 6 Nov '10  
QuestionFiltering? PinmemberRonni Marker2:22 22 Oct '09  
Questionhow to bind the xml file exported from tally to gridview? PinmemberShalini_Jha19:14 17 Aug '09  
Questionhow to insert data ? Pinmembersrikanth ravipati20:21 10 Sep '08  
Questionhow to add controls to gridview if grdi contains xml data? Pinmembersrikanth ravipati20:13 10 Sep '08  
GeneralGrid view does not show any data Pinmemberaspnet2.developer2:30 21 Nov '07  
GeneralRe: Grid view does not show any data Pinmemberaspnet2.developer2:32 21 Nov '07  
GeneralThanks PinmemberTheMara0:35 17 Aug '07  
Generaldoesn't work Pinmemberimperialx20:42 19 Jun '07  
GeneralRe: doesn't work Pinmemberfstrahberger21:04 19 Jun '07  
GeneralRe: doesn't work Pinmemberimperialx1:20 20 Jun '07  
Questionxml PinmemberMember #377932020:18 31 Jan '07  
GeneralEditing the xml contents PinmemberSalam Y. ELIAS8:31 19 Aug '06  
GeneralSimple and to the point PinmemberTinymouse19:02 31 Jul '06  
GeneralRe: Simple and to the point Pinmemberfstrahberger21:44 31 Jul '06  

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.

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