未加载来自XSLT的数据

时间:2015-02-10 18:03:52

标签: c# asp.net xml xslt webforms

我有一个ASP.NET Web窗体应用程序。 我使用XSLT来形成HTML页面。

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html"/>
  <xsl:template match="/">
    <html>
      <head>
        <title>Footballer's XSLT Demo</title>
      </head>
      <body>
        <h2>Footballer's Information</h2>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

我在加载页面上写了这段代码:

namespace XSLT
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string MyXsltPath = Request.PhysicalApplicationPath + "1/footballers.xslt";
            XslCompiledTransform XSLTransform = new XslCompiledTransform();
            XSLTransform.Load(MyXsltPath);
        }
    }
}

但没有显示任何信息。 我该怎么做?

1 个答案:

答案 0 :(得分:0)

以下是加载xslt文件的代码,

            XmlDocument xmlDoc = new XmlDocument();
            System.IO.Stream xmlStream;
            System.Xml.Xsl.XslCompiledTransform xsl = new System.Xml.Xsl.XslCompiledTransform();
            ASCIIEncoding enc = new ASCIIEncoding();
            System.IO.StringWriter writer = new System.IO.StringWriter();

        String TransactionXML = "<xml><node></node></xml>";
        // Get Xsl
        xsl.Load(HttpContext.Current.Server.MapPath("footballers.xslt"));
        // Remove the utf encoding as it causes problems with the XPathDocument
        TransactionXML = TransactionXML.Replace("utf-32", "");
        TransactionXML = TransactionXML.Replace("utf-16", "");
        TransactionXML = TransactionXML.Replace("utf-8", "");
        xmlDoc.LoadXml(TransactionXML);

        // Get the bytes
        xmlStream = new System.IO.MemoryStream(enc.GetBytes(xmlDoc.OuterXml), true);

        // Load Xpath document
        System.Xml.XPath.XPathDocument xp = new System.Xml.XPath.XPathDocument(xmlStream);

        // Perform Transform
        xsl.Transform(xp, null, writer);
        Response.Write(writer.ToString());