How to : ????

How to : Bind XML data runtime with Repeater control in asp.net

For binding XML data with repeater control , XmlDataSource control (XMLDataSource represents an XML data source to data-bound controls) is used. For binding repeater I had two options , first was I can use <asp:xmldatasource> control on design page of asp.net and second was I can use this ' XmlDataSource' class and bind xml data runtime with repeater control.
       In this article, I will explain second method of dynamic process.The Repeater control uses an XPath data-binding expression to bind to data items within the XML document that the XmlDataSource represents.

Code of *.aspx

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
   <title></title>
</head>
<body>
   <form id="form1" runat="server">
   <div>
       <asp:Repeater runat="server" ID="rptTest">
           <ItemTemplate>
               <%# XPath("Name") %><br />
               </strong>
               <%# XPath("Plan") %><br />
               Premium :
               <%# XPath("Premium") %><br />
           </ItemTemplate>
       </asp:Repeater>
   </div>
   </form>
</body>
</html>


Code of  *.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.IO;
namespace TestingNew
{
   public partial class ConsumeAsyncService : System.Web.UI.Page
   {
       protected void Page_Load(object sender, EventArgs e)
       {
           BindRepeater();
       }         
       private void BindRepeater()
       {
           DataSet ds = new DataSet("Insurers");
           DataTable dt = new DataTable("Insurer");

           DataColumn colInsurer = new DataColumn("Name");
           colInsurer.DataType = System.Type.GetType("System.String");
           dt.Columns.Add(colInsurer);
           
           DataColumn colPlan = new DataColumn("Plan");
           colPlan.DataType = System.Type.GetType("System.String");
           dt.Columns.Add(colPlan);

           DataColumn colPremium = new DataColumn("Premium");
           colPremium.DataType = System.Type.GetType("System.String");
           dt.Columns.Add(colPremium);    

           DataColumn colRemark = new DataColumn("Remarks");
           colRemark.DataType = System.Type.GetType("System.String");
           dt.Columns.Add(colRemark);
           
           ds.Tables.Add(dt);

           DataRow dr1 = ds.Tables[0].NewRow();
           dr1["Name"] = "ICICI";
           dr1["Plan"] = "ICare";
           dr1["Premium"] = "2000";
           dr1["Remarks"] = "Test1";
           ds.Tables[0].Rows.Add(dr1);

           DataRow dr2 = ds.Tables[0].NewRow();
           dr2["Name"] = "HDFC";
           dr2["Plan"] = "HDFC Health Care";
           dr2["Premium"] = "3000";
           dr1["Remarks"] = "Test";
           ds.Tables[0].Rows.Add(dr2);                        

           XmlDataSource xml = new XmlDataSource();
           xml.ID = "xmlTest";
           xml.Data = WriteToXML(ds);
        
           rptTest.DataSource = xml;
           rptTest.DataBind();
       }

       private string WriteToXML(DataSet ds)
       {
           StringBuilder sbSQL = new StringBuilder();
           StringWriter swSQL = new StringWriter(sbSQL);
           string XMLFormat;
           
           ds.WriteXml(swSQL);
           
           XMLFormat = swSQL.ToString();

           return XMLFormat;
       }
       
   }
}

In *.aspx.cs code, I have used dataset which contains data.Reason of using dataset is that now I have choice of getting data from database .I can easily get those in dataset. After storing data , convert it in XML.
     Here , we have a choice of using direct any XML file besides of using any dataset and then convert it into XML (if you don't want to use Database).
Create new object of  XmlDataSource and assign ID (you should make sure that  XmlDataSource have unique ID) and set XML data to that object.Finally bind that  XmlDataSource object with repeater. 

2 comments:

  1. very good example but i have a few question.
    1. why you used sbSQL?
    It's not used anywhere else in the following code.
    2. What is the use of string builder in this scenario.

    ReplyDelete
    Replies
    1. Hi neeraj,
      for converting data to XML format I have used StringWriter class which is using StringBuilder object( As new instance of
      StringWriter class writes to the specified StringBuilder object).

      Delete