简体   繁体   中英

How can I use an XSLT to transform XML to HTML in C#?

I am stuck in firstly creating a simple xslt. stylesheet to use for transforming an xml object.Secondly I am stuck in actually using the xslt stylesheet to do the transformation.

The background is the following:

I have a web part which is essentially a small form that has three inputs. These inputs then get submitted and used to query an external API by Http GET request. The results of the query then are displayed on a separate page in XML format. What I now need is to transform the xml to html and output that instead of the XML.

What I currently have:

I have a string variable "tmp" that holds the results from an api query by http get request. The results of the query get stored in the variable and I am able to display the results using: (Code given are small snippets of the whole code which are the most relevant for this particular case)

StreamReader reader = new StreamReader(response.GetResponseStream());
string tmp = reader.ReadToEnd();

Response.Write(tmp);
Response.End();

I then use "tmp" as an XML document object like so:

XmlDocument doc = new XmlDocument();
doc.Load(tmp);

To my project I then added an xslt file that will be used for the transformation.

Here is where I am stuck:

  1. I have created the XML document object as listed above. How do I then proceed to use the XSLT file I have added to my project to do the transformation?

  2. How do I actually achieve the transformation to have the output transformed to HTML.

I have been struggling with this for the best part of a week now.

Here's a simple example of how to use XSLT to transform XML into HTML:

string tmp = "<XML DATA>";
StringBuilder sbXslOutput = new StringBuilder();   

using (XmlWriter xslWriter = XmlWriter.Create(sbXslOutput))  
{  
    XslCompiledTransform transformer = new XslCompiledTransform();  
    transformer.Load("transformer.xsl");  
    XsltArgumentList args = new XsltArgumentList();  

    XmlDataDocument doc = new XmlDataDocument();
    doc.Loadxml(tmp);

    transformer.Transform(doc, args, xslWriter);  
}  

string dataSetHtml = sbXslOutput.ToString();  

Let's say this is your XML:

<RecentMatter>   
  <UserLogin>PSLTP6\RJK</UserLogin>   
  <MatterNumber>99999-2302</MatterNumber>   
  <ClientName>Test Matters</ClientName>   
  <MatterName>DP Test Matter</MatterName>   
  <ClientCode>99999</ClientCode>   
  <OfficeCode/>   
  <OfficeName/>   
  <Billable>true</Billable>   
  <ReferenceId/>   
  <LastUsed>2011-08-23T23:40:24.13+01:00</LastUsed>   
</RecentMatter>   
<RecentMatter>   
  <UserLogin>PSLTP6\RJK</UserLogin>   
  <MatterNumber>999991.0002</MatterNumber>   
  <ClientName>Lathe 1</ClientName>   
  <MatterName>LW Test 2</MatterName>   
  <ClientCode/>   
  <OfficeCode/>   
  <OfficeName/>   
  <Billable>true</Billable>   
  <ReferenceId/>   
  <LastUsed>2011-07-12T16:57:27.173+01:00</LastUsed>   
</RecentMatter>   
<RecentMatter>   
  <UserLogin>PSLTP6\RJK</UserLogin>   
  <MatterNumber>999991-0001</MatterNumber>   
  <ClientName>Lathe 1</ClientName>   
  <MatterName>LW Test 1</MatterName>   
  <ClientCode/>   
  <OfficeCode/>   
  <OfficeName/>   
  <Billable>false</Billable>   
  <ReferenceId/>   
  <LastUsed>2011-07-12T01:59:06.887+01:00</LastUsed>   
</RecentMatter>   
</NewDataSet>   

Here's an XSLT script that transforms the XML to HTML:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">  
  <xsl:template match="/">  
      <table border="1">  
        <tr>  
          <th>User Login</th>  
          <th>Matter Number</th>  
          ...  
        </tr>  
        <xsl:for-each select="NewDataSet/RecentMatter">  
          <tr>  
            <td>  
              <xsl:value-of select="UserLogin"/>  
            </td>  
            <td>  
              <xsl:value-of select="MatterNumber"/>  
            </td>  
            ...  
          </tr>  
        </xsl:for-each>  
      </table>  
  </xsl:template>  
</xsl:stylesheet> 

You can write to a memory stream:

MemoryStream mStream = new MemoryStream();
mXslt.Transform(new XPathDocument(new XmlNodeReader(mXml)), null, mStream );
mStream.Position = 0;
StreamReader mReader = new StreamReader(mStream);
string mOutput = mReader.ReadToEnd();

Use XPathDocument and XslCompiledTransform . They are much faster than XslTransform and XmlDocument . Even if you use an XmlDocument to create the xml, covert it to an XPathDocument for the transform.

Refer this also for more info: Simplest way to transform XML to HTML with XSLT in C#?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM