ASP.NET GridView Tutorial
Lesson 13 of 301 · Using XSLT transform with XMLDataSourceControl Part 5 · Database
Using XSLT transform with XMLDataSourceControl Part 5
Text version of the video
http://csharp-video-tutorials.blogspot.com/2013/02/transform-xml-using-xslt-and-bind-to.html
Slides
http://csharp-video-tutorials.blogspot.com/2013/10/part-5-transform-xml-using-xslt-and.html
All GridView Text Articles
http://csharp-video-tutorials.blogspot.com/p/free-asp.html
All GridView Slides
http://csharp-video-tutorials.blogspot.com/p/blog-page.html
All Dot Net and SQL Server Tutorials in English
https://www.youtube.com/user/kudvenkat/playlists?view=1&sort=dd
All Dot Net and SQL Server Tutorials in Arabic
https://www.youtube.com/c/KudvenkatArabic/playlists
In Part 4 of the asp.net gridview tutorial, we discussed about binding an xml file to a gridview control using xmldatasource control. XmlDataSource control did not work as expected, when the xml file, contained child xml nodes. We had to manually rewrite the entire xml file using attributes instead. However, manually rewriting the xml file is laborious and error prone, especially if the file is very large.
We can use an XSLT transform file, to dynamically convert the child xml nodes to attributes. Countries xml with child nodes is shown below. Create an asp.net web application. Add a folder with name=Data to the project. Right click on the “Data” folder and add an XML file with name=Countries.xml.
Drag “XmlDataSource” control from the toolbox, onto WebForm1.aspx. Click on the smart tag button of the “XmlDataSource” control, and then click on “Configure Data Source”. Click on “Browse” button next to “Data file” textbox and select “Countries.xml” from “Data” folder. Now, click on “Browse” button next to “Transform file” textbox and select “CountriesXSLT.xslt” from “Data” folder. Finally click OK.
Now, drag and drop a gridview control on WebForm1.aspx. Click on smart tag button on the gridview control, and select “XmlDataSource1” from “Choose Data Source” dropdownlist. Notice that, the gridview control displays xml data as expected.
If you want to display the same xml data in a gridview control without using xslt transform, then load the xml data into a dataset and then bind it to the gridview control.
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath(“~/Data/Countries.xml”));
GridView1.DataSource = ds;
GridView1.DataBind();