Thursday, July 21, 2011

XML, XSLT and ASP.Net

Here is the example we did in class. First we created the XSLT document. The XSLT document is designed to take an XML document and transform it into some other kind of document--in our case an html document. Here is a picture of the web page running:



Here is the code for the XSLT:


memo.xslt
<?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"
xmlns="http://www.w3.org/1999/xhtml" xmlns:m ="http://www.spconger.com/memo" >

<xsl:output method="xml" indent="yes"/>

<xsl:template match="/">
<h2>Automart Memo</h2>
<hr/>
<p>
<strong>To: </strong>
<xsl:value-of select="m:memo/m:header/m:to"/>
</p>
<p>
<strong>From: </strong>
<xsl:value-of select="m:memo/m:header/m:from"/>
</p>
<p>
<strong>Date: </strong>
<xsl:value-of select="m:memo/m:header/m:date"/>
</p>
<p>
<strong>About: </strong>
<xsl:value-of select="m:memo/m:header/m:about"/>
</p>
<hr/>
<xsl:for-each select="//m:memo/m:body/m:p">
<p>
<xsl:value-of select="."/>
</p>
</xsl:for-each>
</xsl:template>

</xsl:stylesheet>


We started a new empty Web Site in visual Studio. We added a new web form and then added an existing item--the XSLT document. Here is the code for the Web Form. We added a GridView, a button and an XML control. For the Gridview we also added a new column which was a button, (type link, command Select, text Select) Most of the other markup for the web grid is just formatting and could be left out.


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" BackColor="White"
BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" CellPadding="4">
<Columns>
<asp:ButtonField CommandName="Select" Text="Select" />
</Columns>
<FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
<HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
<PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
<RowStyle BackColor="White" ForeColor="#003399" />
<SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
<SortedAscendingCellStyle BackColor="#EDF6F6" />
<SortedAscendingHeaderStyle BackColor="#0D4AC4" />
<SortedDescendingCellStyle BackColor="#D6DFDF" />
<SortedDescendingHeaderStyle BackColor="#002876" />
</asp:GridView>
<asp:Button ID="Button1" runat="server" Text="Get memo"
onclick="Button1_Click" />
<asp:Xml ID="Xml1" runat="server"></asp:Xml>
</div>
</form>
</body>
</html>


In the code behind we used basic ADO. We needed to Add the System.Data, System.Data.SqlClient and the System.Xml libraries in the using section.
First we create a connection object to connect to the database then a command object to pass sql to the server through the connection. A data reader reads the data into the Grid.

In the button we create a new command that selects the XML file for the selected memo and attaches it to the XML Control. We also call the XSLT file we had made.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data; //add these three libraries
using System.Data.SqlClient;
using System.Xml;

public partial class _Default : System.Web.UI.Page
{
SqlConnection connect;
protected void Page_Load(object sender, EventArgs e)
{
connect = new SqlConnection("Data Source=localhost;initial catalog=Automart;user=EmployeeLogin;password=pass");
string sql = "Select AutomartMemoID, MemoDescription from AutomartMemos";
SqlCommand cmd = new SqlCommand(sql, connect);
SqlDataReader reader = null;
connect.Open();
reader = cmd.ExecuteReader();
GridView1.DataSource = reader;
GridView1.DataBind();
reader.Dispose();
connect.Close();
}
protected void Button1_Click(object sender, EventArgs e)
{
XmlDocument xDoc = new XmlDocument();
string sql = "Select memo from AutomartMemos where AutomartMemoID=@ID";
SqlCommand cmd = new SqlCommand(sql, connect);
cmd.Parameters.AddWithValue("@ID", int.Parse(GridView1.SelectedRow.Cells[1].Text));
XmlReader xreader = null;
connect.Open();
xreader = cmd.ExecuteXmlReader();
xDoc.Load(xreader);
xreader.Close();
connect.Close();

Xml1.Document = xDoc;
Xml1.TransformSource = MapPath("memo.xslt");

}
}

1 comment:




  1. Just I have given all details about Hyvee connect Login, Hyvee connect Register, Hyvee connect forgot password
    and all the information which you are looking related hy vee login or hy vee employee login.


    Hyvee connect Login



    Hyvee connect forgot password



    hy vee login or hy vee employee login.



    Hyvee connect Login



    Hyvee connect forgot password


    hy vee login or hy vee employee login.

    ReplyDelete