Udostępnij przez


Stosowanie transformacja XSL (klasy Managed SQLXML)

W tym przykładzie kwerenda SQL jest wykonywany w bazie danych AdventureWorks.XSL Transformation Jest stosowany do wyników kwerendy do generowania dwie kolumna tabela pracowników pierwszego i ostatniego nazwiska.

The XslPath właściwość of the SqlXmlCommand object is used to specify the XSL file and its directory ścieżka.

Uwaga

W kodzie musisz podać nazwę wystąpienie programu Microsoft SQL Server w ciąg połączenia.

using System;
using Microsoft.Data.SqlXml;
using System.IO;
class Test
{
      static string ConnString = "Provider=SQLOLEDB;Server=(local);database=AdventureWorks;Integrated Security=SSPI";
      public static int testXSL()
      {
         //Stream strm;
         SqlXmlCommand cmd = new SqlXmlCommand(ConnString);
         cmd.CommandText = "SELECT TOP 20 FirstName, LastName FROM Person.Contact FOR XML AUTO";
         cmd.XslPath = "MyXSL.xsl";
         cmd.RootTag = "root";
         using (Stream strm = cmd.ExecuteStream()){
            using (StreamReader sr = new StreamReader(strm)){
               Console.WriteLine(sr.ReadToEnd());
            }
        }
         return 0;
      }
      public static int Main(String[] args)
      {
         testXSL();   
         return 0;
      }
   }

Jest to arkusz stylów XSL, można użyć do testowania aplikacji:

<?xml version='1.0' encoding='UTF-8'?>
 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
    <xsl:output method="html"/>
    <xsl:template match = '*'>
        <xsl:apply-templates />
    </xsl:template>
    <xsl:template match = 'Person.Contact'>
       <TR>
         <TD><xsl:value-of select = '@FirstName' /></TD>
         <TD><B><xsl:value-of select = '@LastName' /></B></TD>
       </TR>
    </xsl:template>
    <xsl:template match = '/'>
      <HTML>
        <HEAD>
           <STYLE>th { background-color: #CCCCCC }</STYLE>
        </HEAD>
        <BODY>
         <TABLE border='1' style='width:300;'>
           <TR><TH colspan='2'>Contacts</TH></TR>
           <TR><TH >First name</TH><TH>Last name</TH></TR>
           <xsl:apply-templates select = 'root' />
         </TABLE>
        </BODY>
      </HTML>
    </xsl:template>
</xsl:stylesheet>

Aby przetestować aplikację

  1. Arkusz stylów XSL można zapisać w pliku (MyXSL.xsl).

  2. Zapisz C# kod (DocSample.cs), który znajduje się w tym przykładzie, w tym samym folderze, w którym przechowywany jest arkusz stylów.

  3. Skompiluj kod.Aby skompilować kod w wiersz polecenia, należy użyć:

    csc /reference:Microsoft.Data.SqlXML.dll DocSample.cs
    

    Spowoduje to utworzenie pliku wykonywalnego (DocSample.exe).

  4. W wiersz polecenia należy wykonać DocSample.exe.

Stosowanie XSL Transformation w programie .NET Framework

Zamiast stosować XSL Transformation w warstwie środkowej, jak opisano wcześniej, można zastosować XSL Transformation (w programie .NET Framework) po stronie klient.W poniższym poprawioną C# kodzie pokazano sposób stosowania transformacja XSL w programie .NET Framework.

Uwaga

W kodzie musisz podać nazwę wystąpienie programu SQL Server w ciąg połączenia.

using System;
using System.Xml;
using Microsoft.Data.SqlXml;
using System.IO;
using System.Xml.XPath;
using System.Xml.Xsl;

class Test
{
      static string ConnString = "Provider=SQLOLEDB;Server=(local);database=AdventureWorks;Integrated Security=SSPI";
      public static int testXSL()
      {
         //Stream strm;
         SqlXmlCommand cmd = new SqlXmlCommand(ConnString);
         cmd.CommandText = "SELECT TOP 20 FirstName, LastName FROM Person.Contact FOR XML AUTO";
         cmd.RootTag = "root";
         using (Stream strm = cmd.ExecuteStream()){
            XmlReader reader = new XmlReader(strm);
            XPathDocument xd = new XPathDocument(reader, XmlSpace.Preserve);
            XslCompiledTransform xslt = new XslCompiledTransform();
            xslt.Load("MyXSL.xsl");
            XmlWriter writer = XmlWriter.Create("xslt_output.html");
            xslt.Transform(xd, writer);
            reader.Close();
            writer.Close();
         }
         return 0;
      }
      public static int Main(String[] args)
      {
         testXSL();   
         return 0;
      }
   }