CSharp - LINQ XML XSLT Transform

Introduction

First we create the XSLT from string.

string xsl =
  @"<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1990/XSL/Transform'>
    <xsl:template match='//Books'>
        <html>
            <body>
            <h1>Book Participants</h1>
            <table>
                <tr align='left'>
                    <th>Role <th>First Name<th>Last Name                </tr>
                <xsl:apply-templates></xsl:apply-templates>
            </table>
            </body>
              </html>
          </xsl:template>
          <xsl:template match='Book'>
              <tr>
                  <td><xsl:value-of select='@type'/></td>
                  <td><xsl:value-of select='FirstName'/></td>
                  <td><xsl:value-of select='LastName'/></td>
              </tr>
          </xsl:template>
        </xsl:stylesheet>";

Then we build the XML data.

XDocument xDocument = new XDocument(
  new XElement("Books",
    new XElement("Book",
      new XAttribute("type", "Author"),
      new XElement("FirstName", "Joe"),
      new XElement("LastName", "Ruby")),
    new XElement("Book",
      new XAttribute("type", "Editor"),
      new XElement("FirstName", "PHP"),
      new XElement("LastName", "Python"))));

We need to create a new XDocument for the transformed version.

From that document, we will create an XmlWriter, instantiate an XslCompiledTransform object, load the transform object with the transformation style sheet, and transform our input XML document into the output XmlWriter:

XDocument transformedDoc = new XDocument();
using (XmlWriter writer = transformedDoc.CreateWriter())
{
  XslCompiledTransform transform = new XslCompiledTransform();
  transform.Load(XmlReader.Create(new StringReader(xsl)));
  transform.Transform(xDocument.CreateReader(), writer);
}
Console.WriteLine(transformedDoc);

Demo

using System;
using System.Linq;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Xml.Xsl;
using System.Xml;
using System.IO;/*ww w .  jav a2 s .  c  o  m*/

class Program
{
    static void Main(string[] args)
    {

        string xsl =
          @"<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1990/XSL/Transform'>
    <xsl:template match='//Books'>
        <html>
            <body>
            <h1>Book Participants</h1>
            <table>
                <tr align='left'>
                    <th>Role                    <th>First Name
<th>Last Name                </tr>
                <xsl:apply-templates></xsl:apply-templates>
            </table>
            </body>
              </html>
          </xsl:template>
          <xsl:template match='Book'>
              <tr>
                  <td><xsl:value-of select='@type'/></td>
                  <td><xsl:value-of select='FirstName'/></td>
                  <td><xsl:value-of select='LastName'/></td>
              </tr>
          </xsl:template>
        </xsl:stylesheet>";

        XDocument xDocument = new XDocument(
          new XElement("Books",
            new XElement("Book",
              new XAttribute("type", "Author"),
              new XElement("FirstName", "Joe"),
              new XElement("LastName", "Ruby")),
            new XElement("Book",
              new XAttribute("type", "Editor"),
              new XElement("FirstName", "PHP"),
              new XElement("LastName", "Python"))));

        XDocument transformedDoc = new XDocument();
        using (XmlWriter writer = transformedDoc.CreateWriter())
        {
            XslCompiledTransform transform = new XslCompiledTransform();
            transform.Load(XmlReader.Create(new StringReader(xsl)));
            transform.Transform(xDocument.CreateReader(), writer);
        }
        Console.WriteLine(transformedDoc);



    }
}

Result