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