C#程序中的數(shù)據(jù)顯示:自定義標(biāo)簽和XML、XSL
通過在C#程序中定義標(biāo)簽類,然后在頁面進(jìn)行數(shù)據(jù)綁定,從而實(shí)現(xiàn)數(shù)據(jù)和顯示的分離,顯示的樣式采用XSL定義。數(shù)據(jù)實(shí)體通過序列化為XML,然后用XSL解析,形成HTML內(nèi)容。對(duì)于其中需要實(shí)現(xiàn)安全特性,防范XSS攻擊,采用XSL自定義FUNCTION進(jìn)行ENCODE。
C#程序中的數(shù)據(jù)顯示實(shí)現(xiàn)代碼
- public class Encoding
- {
- public string Encode(string cSource)
- {
- return System.Web.HttpUtility.HtmlEncode(cSource);//可以使用Microsoft XSS LIB
- }
- }
- public class EmList : Label
- {
- public override bool EnableViewState
- {
- get{ return false;}
- }
- public string XslFile{get;set;}
- public object SerialObject{get;set;}
- protected override void Render(HtmlTextWriter writer)
- {
- if (SerialObject == null)
- {
- throw new Exception("對(duì)象未初始化");
- }
- System.Xml.Serialization.XmlSerializer oSerial = new System.Xml.Serialization.XmlSerializer(SerialObject.GetType());
- System.Text.StringBuilder oSb = new System.Text.StringBuilder();
- System.IO.StringWriter oWr = new System.IO.StringWriter(oSb);
- string Xml = "";
- oSerial.Serialize(oWr, SerialObject);
- Xml =oSb.ToString();
- string cXslFileName = this.MapPathSecure(XslFile);
- if (!System.IO.File.Exists(cXslFileName))
- {
- throw new Exception("請(qǐng)加自己的處理異常程序");
- }
- System.Xml.Xsl.XsltArgumentList xslArgs = new System.Xml.Xsl.XsltArgumentList();
- Encoding oEn = new Encoding();
- xslArgs.AddExtensionObject("urn:Encoding", oEn);
- System.Xml.XmlDocument oDoc = new System.Xml.XmlDocument();
- try
- {
- oDoc.LoadXml(Xml);
- }
- catch
- {
- throw new Exception("請(qǐng)加自己的處理異常程序");
- }
- System.Xml.Xsl.XslCompiledTransform oTran = new System.Xml.Xsl.XslCompiledTransform();
- string cXsl = "";
- try
- {
- cXsl = System.IO.File.ReadAllText(cXslFileName);
- }
- catch
- {
- throw new Exception("請(qǐng)加自己的處理異常程序");
- }
- System.IO.StringReader oSr=new System.IO.StringReader(cXsl);
- System.Xml.XmlReader oRe=System.Xml.XmlReader.Create(oSr);
- try
- {
- oTran.Load(oRe);
- }
- catch
- {
- throw new Exception("請(qǐng)加自己的處理異常程序");
- }
- try
- {
- oTran.Transform(oDoc, xslArgs, writer);
- }
- catch
- {
- throw new Exception("請(qǐng)加自己的處理異常程序");
- }
- }
- }
- public class PageBar : System.Web.UI.HtmlControls.HtmlControl
- {
- public int PageNum{get;set;}
- public int PageSize { get; set; }
- public int PageCount { get; set; }
- public string BaseUrl{get;set;}
- protected override void Render(HtmlTextWriter writer)
- {
- writer.Write(string.Format("< a href={0}?PageNum=1>第一頁< /a>|< a href={0}?PageNum={1}>上一頁< /a>|< a href={0}?PageNum={2}>下一頁< /a>|< a href={0}?PageNum={3}>尾頁< /a> (共{4}當(dāng)前頁{5})", BaseUrl, PageNum - 1 > 0 ? PageNum - 1 : 1, PageNum + 1 > PageCount ? PageCount : PageNum + 1, PageCount, PageCount, PageNum));
- }
- }
C#程序中的數(shù)據(jù)顯示:頁面定義
- < %@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
- < %@Register TagPrefix="CS" Namespace="WebApplication1.Control" Assembly=" WebApplication1" %>
- < !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>
- < CS:EmList SerialObject="< %#List%>" XslFile="XSL/test.xslt" runat="server" />< br />
- < CS:PageBar PageNum="< %#PageNum%>" BaseUrl="< %#Request.Path%>" PageCount="5" runat="server" />
- < /body>
- < /html>
C#程序中的數(shù)據(jù)顯示:其中List和PageNum為頁面屬性
XSLT:
- < xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform " version="1.0" xmlns:Encoding="urn:Encoding">
- < xsl:output method="text" />
- < xsl:template match="/">
- < h2>
- < xsl:for-each select="ArrayOfEmployeeEntity/EmployeeEntity">
- < ![CDATA[< a href="http://www.csdn.net/Employee.aspx?ID=]]>< xsl:value-of select="Encoding:Encode ( EmployeeID)"/>< ![CDATA[" alt="]]>< xsl:value-of select= "Encoding:Encode(Full_Name)"/>< ![CDATA[">< br/>]]>< xsl:value-of select= "Encoding:Encode(Email_Address)"/>< /xsl:for-each>
- < /h2>
- < /xsl:template>
- < /xsl:stylesheet>
以上就是通過自定義標(biāo)簽和XML、XSL實(shí)現(xiàn)C#程序中數(shù)據(jù)的顯示,希望對(duì)大家有所幫助。
【編輯推薦】