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
 

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.
Search this forum  
    Spacing  Noise  Layout  Per page   
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 
GeneralThanks PinmemberTheMara17 Aug '07 - 0:35 
Generaldoesn't work Pinmemberimperialx19 Jun '07 - 20:42 
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 

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

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