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.

XMLDataSource

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" 
              AutoGenerateColumns="False" 
              DataSourceID="XmlDataSource1">
<Columns>
<asp:BoundField DataField="FirstName" HeaderText="FirstName" 
                SortExpression="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" 
                SortExpression="LastName" />
<asp:BoundField DataField="Location" HeaderText="Location" 
                SortExpression="Location" />
</Columns>
</asp:GridView>

<asp:XmlDataSource ID="XmlDataSource1" runat="server" 
                 DataFile="~/App_Data/XMLFile.xml" 
                 TransformFile="~/App_Data/XSLTFile.xslt">
</asp:XmlDataSource>

Build and run the application.

Download Sample Code


If you like this post than join us or share

3 comments:

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 c#.net with out using database. this am doing for intranet ,my query is:
I have created a folder called " backup " in app_data ..in my application,in that folder am uploading files using file-upload..according to the needs..now 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 example..in app_data i have backup folder...in that folder i have 3 files for example...call 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.";
}
else
{
try
{
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 ...so this process will continue daily...now 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..
thanks
kalyan


Unknown said...

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

Hope this solves your problem


Find More Articles