GridView XMLDataSource Example

GridView XMLDataSource Example. In this post i'm explaining how to use XML file or XML data as XMLDataSource to populate GridView in Asp.Net 2.0,3.5,4.0.

GridView XMLDataSource Example

For this example i have created a simple xml file and added it in App_Data Folder of Asp.Net application.

The data in XML file look like shown below.

Amit Jain Mumbai
user 1 Delhi
User 2 Noida
User 3 Bangalore

First of All Add a GridView on aspx page and click on smart tag in design view of page and select new data source.
Browse to xml file path and click on ok.


When we click on ok we get error as displayed in the image.

XMLDataSource Error

The data source for GridView with id 'GridView1' did not have any properties or attributes from which to generate columns. Ensure that your data source has content.

This error is caused because the XML data is not in the format gridview can read.

GridView needs XML data in below mentioned format.


To fix this error we need to provide XSLT Schema. Right click on solution explorer and select add new item, from new dialog box that opens, select XSLT file.

Now we need to provide XML template in this XSLT file which should look like mentioned below.



HTML Source of GridView
<asp:GridView ID="GridView1" runat="server" 
<asp:BoundField DataField="FirstName" HeaderText="FirstName" 
                SortExpression="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" 
                SortExpression="LastName" />
<asp:BoundField DataField="Location" HeaderText="Location" 
                SortExpression="Location" />

<asp:XmlDataSource ID="XmlDataSource1" runat="server" 

Build and run the application.

Download Sample Code

If you like this post than join us or share


Anonymous said...

That's really interesting. Thanks for posting all the great information! Had never thought of it all that way before.

kalyan123 said...

i want solution for this friends.
In my application am using with out using database. this am doing for intranet ,my query is:
I have created a folder called " backup " in app_data my application,in that folder am uploading files using file-upload..according to the i want to display those files in data grid with link button or only link button..if i click that link button the file should save in my file system like save as...what are the files in backup folder should be displayed as links...step by step..
any one please help me in this...

just for app_data i have backup that folder i have 3 files for as a.txt,b.txt.c.txt...these files should display in my page ...the file is not necessary to open ...i just want to show all files in page. if i click any file it should be saved in file system...

for uploading i used this code

protected void Btnoffline_Click(object sender, EventArgs e)
if (Flpload.PostedFile.FileName == "")
lblstatus.Text = "No file specified.";
string serverFileName = Path.GetFileName(Flpload.PostedFile.FileName);

Flpload.PostedFile.SaveAs(MapPath("~/App_Data/backup/") + serverFileName);
lblstatus.Text = "File " + serverFileName;
lblstatus.Text += " backup file uploaded successfully.";
catch (Exception err)
lblstatus.Text = err.Message;


now here upload of backup file is over this process will continue next issue is that to see those uploaded files some where in any control or page...and if click those files it should save some where..
if u have good any good idea resolving this ..plz share to me...i will develop like that..

amiT jaiN said...

@kalyan123: Please read Download File From Server In Asp.Net

Hope this solves your problem

Find More Articles