Binding XML Data to a WebForm Control in ASP.Net

Suppose we have the requirement that a web site needs to create a Web Application to store the details in the XML data format of the products available for sale. The details of the products should be displayed in a data grid. The details include product id, product name, description, price, quantity available, and category. The form should have an advertisement banner. When the user clicks the banner, the order form of the WebShoppe Website should be displayed.
Let's have a look at the following steps:
  1. Open the Visual Studio 2010.
  2. Now go to File -> New -> WebSite.
  3. Now give the name to your application and click the ok button. A Web Application will be created.
  4. Now right-click on the name of the project in the Solution Explorer and select the Add New Item option from it.
  5. Now select the XML file template from the available templates from the Add New Item dialog box.
  6. Give the name to this file as Product.xml and select the Add button.
  7. Now open the file and add the following code in it:
    1. <?xml version="1.0" encoding="utf-8" ?>  
    2. <productdata>  
    3.   <product category="toy">  
    4.     <prodid>  
    5.       P001  
    6.     </prodid>  
    7.     <productname>Mini Bus</productname>  
    8.     <description>This is a toy for children aged 4 and above</description>  
    9.     <price>75</price>  
    10.     <qtyavilable>54</qtyavilable>  
    11.   </product>  
    12.   <product category="toy">  
    13.     <prodid>  
    14.       P002  
    15.     </prodid>  
    16.     <productname>Barbie Doll</productname>  
    17.     <description>This is a toy for children in the age group of 5-10</description>  
    18.     <price>100</price>  
    19.     <qtyavilable>200</qtyavilable>  
    20.   </product>  
    21. </productdata> 
  8. Now to display the XML data in a GridView just add a GridView control on the Webform; see:
  9. To make the format of the GridView just click on the smart button appearing aside the GridView control and select the Autoformat option, as in:
  10. Select any color scheme from the available schemes and click the ok button.
  11. The scheme will be applied to this GridView control.
  12. Now to display banner advertisements on the web page just add an AdRotator control from the toolbox to the myform.aspx file.
  13. Now write the following code in the page_load event to bind the XML document to the GridView control.
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Web;  
    5. using System.Web.UI;  
    6. using System.Web.UI.WebControls;  
    7. using System.Data.SqlClient;  
    8. using System.Data;   
    9. public partial class myform : System.Web.UI.Page  
    10. {  
    11.     protected void Page_Load(object sender, EventArgs e)  
    12.     {  
    13.         DataSet ds = new DataSet();  
    14.         ds.ReadXml(MapPath("Product.xml"));  
    15.         GridView1.DataSource = ds;  
    16.         GridView1.DataBind();  
    17.     }  
  14. Now to write the code for the advertisement file just again right-click on the project name and select Add New Item.
  15. Select XML File from the available templates in the Add New Item Dialog Box and give the name to the file as advertis.xml and click on the add button.
  16. Now add the following code in it to create an advertisement file:
    1. <?xml version="1.0" encoding="utf-8" ?>  
    2. <Advertisements>  
    3.   <Ad>  
    4.     <ImageUrl>http://localhost:1330/productdetails/Images/myshopping.jpg</ImageUrl>  
    5.     <NavigateUrl>http://localhost:1330/productdetails/Onlineshopping.aspx</NavigateUrl>  
    6.     <AlternateText>The Order Online Form for WebShoppe</AlternateText>  
    7.     <Impressions>1</Impressions>  
    8.   </Ad>  
    9.   <Ad>  
    10.     <ImageUrl>http://localhost:1330/productdetails/Images/barbiel2.jpg</ImageUrl>  
    11.     <NavigateUrl>http://localhost:1330/productdetails/Onlineshopping.aspx</NavigateUrl>  
    12.     <AlternateText>The Order Online Form for WebShoppe</AlternateText>  
    13.     <Impressions>2</Impressions>  
    14.   </Ad>  
    15. </Advertisements> 
  17. Now select the AdRotator control on the web form and open the property window of it.
  18. In it select the Advertisement File property of it and click on the browse button.
  19. Now select the appropriate XML file i.e. advertis.xml file and click the ok button.
  20. Now press F5 to run the application.
  21. The output will be as follows:
  22. In it, the XML data of the product file is showing in the GridView and a banner is displaying.
  23. When we will click this banner we will be redirected to the onlineshopping web form.