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"?>

My XLST File Name Is Tests.Xsl

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl=""
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">

  <xsl:template match="/">
    <table border="1">
      <xsl:for-each select="Test/Tests">
              <xsl:value-of select="name"/>
              <xsl:value-of select="cost"/>

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

<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
     <asp:gridview ID="Gridview1" runat="server"


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



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();
            XslTransform docXSL = new XslTransform();
            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
            Gridview1.DataSource = DataSet.Tables[0].DefaultView;
This is The Solution


No comments:

Post a Comment