Friday, August 20, 2010

How To Fill The Grid Using XML and XSLT

Today Topic is Fill The Grid Using XML and XSLT
                 I have taken help from my Senior.
                 1> You Need To Create XML File.
                 2> You Need to Create XSLT it Mean schema File Using XSLT.
                 3> After that take the XML Control in Your .aspx Page.
                 this is the process to achieve the desire result.
                 if you are facing any Problem please send me a message i am trying to solve that problem

My XML File Name Is Text.XML

<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xsl" herf="TestDemo.xsl"?>
<Test>
  <Tests>
    <name>C++</name>
    <cost>2000</cost>
  </Tests>
  <Tests>
    <name>DotNet</name>
    <cost>10000</cost>
  </Tests>
</Test>

My XLST File Name Is Tests.Xsl

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">

  <xsl:template match="/">
    <table border="1">
      <tr>
        <th>Name</th>
        <th>Price</th>
      </tr>
      <xsl:for-each select="Test/Tests">
        <tr>
          <td>
            <br>
              <xsl:value-of select="name"/>
            </br>
          </td>
          <td>
            <br>
              <xsl:value-of select="cost"/>
            </br>
          </td>
        </tr>
      </xsl:for-each>
    </table>
  </xsl:template>
</xsl:stylesheet>

My .aspx page name is Default.aspx

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
     <asp:gridview ID="Gridview1" runat="server"
            AutoGenerateColumns="True"
           >
    </asp:gridview>

 

        <asp:Xml ID="Xml1" runat="server"></asp:Xml>

 

    </div>
    </form>
</body>
</html>

And last but not the list  My .CS File name is Default.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.Xml;
using System.Xml.Xsl;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            XmlDocument docXML = new XmlDocument();
            docXML.Load(Server.MapPath("Test.xml"));
            XslTransform docXSL = new XslTransform();
            docXSL.Load(Server.MapPath("Tests.xslt"));
            Xml1.Document = docXML;
            Xml1.Transform = docXSL;

            DataSet DataSet;
            string filePath = Server.MapPath("Test.xml");
            DataSet = new DataSet();
            //Read the contents of the XML file into the DataSet
            DataSet.ReadXml(filePath);
            Gridview1.DataSource = DataSet.Tables[0].DefaultView;
            Gridview1.DataBind();
        }
       
    }
}
This is The Solution

Cheers
Bindas....

No comments:

Post a Comment