Udostępnij przez


Stosowanie XSL Transformation (SQLXML zarządzane klasy)

W tym przykładzie wykonywana jest kwerenda SQL AdventureWorks2008R2 bazy danych.transformacja XSL jest stosowany do wyników kwerendy, aby wygenerować dwu -kolumna tabela pracowników imiona i nazwiska.

XslPath Właściwość SqlXmlCommand obiektu jest używany do określenia pliku XSL i jego katalogu ścieżka.

Ostrzeżenie

W kodzie, należy podać nazwę wystąpienie programu Microsoft SQL Server w połączeniu z ciąg.

using System;
using Microsoft.Data.SqlXml;
using System.IO;
class Test
{
      static string ConnString = "Provider=SQLOLEDB;Server=(local);database=AdventureWorks2008R2;Integrated Security=SSPI";
      public static int testXSL()
      {
         //Stream strm;
         SqlXmlCommand cmd = new SqlXmlCommand(ConnString);
         cmd.CommandText = "SELECT TOP 20 FirstName, LastName FROM Person.Person 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;
      }
   }

To jest arkusz stylów XSL, można używać 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.Person'>
       <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. Upewnij się, że Microsoft .NET Framework jest zainstalowana na komputerze.Aby przetestować ten przykład jest wymagane.

  2. Zapisz arkusz stylów XSL w pliku (MyXSL.xsl).

  3. Zapisz kod C# (DocSample.cs) dostarczonego w tym przykładzie w tym samym folderze, w którym przechowywany jest arkusz stylów.

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

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

    Tworzy plik wykonywalny (DocSample.exe).

  5. W wiersz polecenia wykonać DocSample.exe.

Stosowanie XSL Transformation w.NET Framework

Zamiast stosować transformacja XSL w warstwie środkowej, jak opisano wcześniej, można zastosować transformacja XSL po stronie klient (w.NET Framework).Następujące poprawioną kod C# pokazuje, jak stosowane jest transformacja XSL w.NET Framework.

Ostrzeżenie

W kodzie, musisz podać nazwę wystąpienie SQL Server w połączeniu z ciąg.

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=AdventureWorks2008R2;Integrated Security=SSPI";
      public static int testXSL()
      {
         //Stream strm;
         SqlXmlCommand cmd = new SqlXmlCommand(ConnString);
         cmd.CommandText = "SELECT TOP 20 FirstName, LastName FROM Person.Person 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;
      }
   }