The resultant HTML string or file can then be rendered as a PDF using the .NET PDF Generator. You can download a sample project showcasing IronPDF's capabilities for converting XML to PDF from this XML to PDF Conversion Example.

// XSLT template that defines the transformation from XML to HTML
string xslt = @"<?xml version='1.0' encoding='UTF-8'?>
<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:template match='/'>
<html>
<style>
td{
  text-align: center;
  padding: 20px;
  border: 1px solid #CDE7F0;
}
th{
  color: white;
  padding: 20px;
}
</style>
<body style='font-family: Arial, Helvetica Neue, Helvetica, sans-serif;'>
  <table style='border-collapse: collapse;'>
    <thead>
      <tr>
        <th colspan='3'>
          <img style='margin: auto;' src='https://ironsoftware.com/img/svgs/ironsoftware-logo-black.svg'/>
        </th>
      </tr>
    </thead>
    <tbody>
      <tr bgcolor='#9acd32'>
        <th bgcolor='#32ab90'>Title</th>
        <th bgcolor='#f49400'>Feature</th>
        <th bgcolor='#2a95d5'>Compatible</th>
      </tr>
      <xsl:for-each select='catalog/cd'>
      <tr>
        <td style='font-weight: bold;'><xsl:value-of select='title'/></td>
        <td style='background-color: #eff8fb; color: #2a95d5; font-weight: bold;'><xsl:value-of select='feature'/></td>
        <td><xsl:value-of select='compatible'/></td>
      </tr>
      </xsl:for-each>
    </tbody>
  </table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
";

// XML data to transform
string xml = @"<?xml version='1.0' encoding='UTF-8'?>
<catalog>
  <cd>
    <title>IronPDF</title>
    <feature>Generate, format and manipulate PDFs</feature>
    <compatible>Microsoft Windows, Linux (Debian, CentOS, Ubuntu), MacOS, Docker (Windows, Linux, Azure), Azure (VPS, Webapps, Websites, Functions), AWS</compatible>
  </cd>
  <cd>
    <title>IronOCR</title>
    <feature>OCR engine, input, result</feature>
    <compatible>Microsoft Windows, Linux, MacOS, Docker, Azure, AWS</compatible>
  </cd>
  <cd>
    <title>IronBarcode</title>
    <feature>Format, read and write Barcode</feature>
    <compatible>Microsoft Windows, Linux, MacOS, Docker, Azure, AWS</compatible>
  </cd>
</catalog>
";

// Create an instance of XslCompiledTransform
XslCompiledTransform transform = new XslCompiledTransform();

// Load the XSLT from a string
using (XmlReader reader = XmlReader.Create(new StringReader(xslt)))
{
    transform.Load(reader);
}

// Transform the XML to HTML
StringWriter results = new StringWriter();
using (XmlReader reader = XmlReader.Create(new StringReader(xml)))
{
    transform.Transform(reader, null, results);
}

// Create a renderer for converting HTML to PDF
IronPdf.ChromePdfRenderer Renderer = new IronPdf.ChromePdfRenderer();
// Options, headers, and footers may be set here if needed
// Render our XML as a PDF via XSLT transformation
Renderer.RenderHtmlAsPdf(results.ToString()).SaveAs("Final.pdf");
// XSLT template that defines the transformation from XML to HTML
string xslt = @"<?xml version='1.0' encoding='UTF-8'?>
<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:template match='/'>
<html>
<style>
td{
  text-align: center;
  padding: 20px;
  border: 1px solid #CDE7F0;
}
th{
  color: white;
  padding: 20px;
}
</style>
<body style='font-family: Arial, Helvetica Neue, Helvetica, sans-serif;'>
  <table style='border-collapse: collapse;'>
    <thead>
      <tr>
        <th colspan='3'>
          <img style='margin: auto;' src='https://ironsoftware.com/img/svgs/ironsoftware-logo-black.svg'/>
        </th>
      </tr>
    </thead>
    <tbody>
      <tr bgcolor='#9acd32'>
        <th bgcolor='#32ab90'>Title</th>
        <th bgcolor='#f49400'>Feature</th>
        <th bgcolor='#2a95d5'>Compatible</th>
      </tr>
      <xsl:for-each select='catalog/cd'>
      <tr>
        <td style='font-weight: bold;'><xsl:value-of select='title'/></td>
        <td style='background-color: #eff8fb; color: #2a95d5; font-weight: bold;'><xsl:value-of select='feature'/></td>
        <td><xsl:value-of select='compatible'/></td>
      </tr>
      </xsl:for-each>
    </tbody>
  </table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
";

// XML data to transform
string xml = @"<?xml version='1.0' encoding='UTF-8'?>
<catalog>
  <cd>
    <title>IronPDF</title>
    <feature>Generate, format and manipulate PDFs</feature>
    <compatible>Microsoft Windows, Linux (Debian, CentOS, Ubuntu), MacOS, Docker (Windows, Linux, Azure), Azure (VPS, Webapps, Websites, Functions), AWS</compatible>
  </cd>
  <cd>
    <title>IronOCR</title>
    <feature>OCR engine, input, result</feature>
    <compatible>Microsoft Windows, Linux, MacOS, Docker, Azure, AWS</compatible>
  </cd>
  <cd>
    <title>IronBarcode</title>
    <feature>Format, read and write Barcode</feature>
    <compatible>Microsoft Windows, Linux, MacOS, Docker, Azure, AWS</compatible>
  </cd>
</catalog>
";

// Create an instance of XslCompiledTransform
XslCompiledTransform transform = new XslCompiledTransform();

// Load the XSLT from a string
using (XmlReader reader = XmlReader.Create(new StringReader(xslt)))
{
    transform.Load(reader);
}

// Transform the XML to HTML
StringWriter results = new StringWriter();
using (XmlReader reader = XmlReader.Create(new StringReader(xml)))
{
    transform.Transform(reader, null, results);
}

// Create a renderer for converting HTML to PDF
IronPdf.ChromePdfRenderer Renderer = new IronPdf.ChromePdfRenderer();
// Options, headers, and footers may be set here if needed
// Render our XML as a PDF via XSLT transformation
Renderer.RenderHtmlAsPdf(results.ToString()).SaveAs("Final.pdf");
' XSLT template that defines the transformation from XML to HTML

Dim xslt As String = "<?xml version='1.0' encoding='UTF-8'?>

<xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>

<xsl:template match='/'>

<html>

<style>

td{

  text-align: center;

  padding: 20px;

  border: 1px solid #CDE7F0;

}

th{

  color: white;

  padding: 20px;

}

</style>

<body style='font-family: Arial, Helvetica Neue, Helvetica, sans-serif;'>

  <table style='border-collapse: collapse;'>

    <thead>

      <tr>

        <th colspan='3'>

          <img style='margin: auto;' src='https://ironsoftware.com/img/svgs/ironsoftware-logo-black.svg'/>

        </th>

      </tr>

    </thead>

    <tbody>

      <tr bgcolor='#9acd32'>

        <th bgcolor='#32ab90'>Title</th>

        <th bgcolor='#f49400'>Feature</th>

        <th bgcolor='#2a95d5'>Compatible</th>

      </tr>

      <xsl:for-each select='catalog/cd'>

      <tr>

        <td style='font-weight: bold;'><xsl:value-of select='title'/></td>

        <td style='background-color: #eff8fb; color: #2a95d5; font-weight: bold;'><xsl:value-of select='feature'/></td>

        <td><xsl:value-of select='compatible'/></td>

      </tr>

      </xsl:for-each>

    </tbody>

  </table>

</body>

</html>

</xsl:template>

</xsl:stylesheet>

"



' XML data to transform

Dim xml As String = "<?xml version='1.0' encoding='UTF-8'?>

<catalog>

  <cd>

    <title>IronPDF</title>

    <feature>Generate, format and manipulate PDFs</feature>

    <compatible>Microsoft Windows, Linux (Debian, CentOS, Ubuntu), MacOS, Docker (Windows, Linux, Azure), Azure (VPS, Webapps, Websites, Functions), AWS</compatible>

  </cd>

  <cd>

    <title>IronOCR</title>

    <feature>OCR engine, input, result</feature>

    <compatible>Microsoft Windows, Linux, MacOS, Docker, Azure, AWS</compatible>

  </cd>

  <cd>

    <title>IronBarcode</title>

    <feature>Format, read and write Barcode</feature>

    <compatible>Microsoft Windows, Linux, MacOS, Docker, Azure, AWS</compatible>

  </cd>

</catalog>

"



' Create an instance of XslCompiledTransform

Dim transform As New XslCompiledTransform()



' Load the XSLT from a string

Using reader As XmlReader = XmlReader.Create(New StringReader(xslt))

	transform.Load(reader)

End Using



' Transform the XML to HTML

Dim results As New StringWriter()

Using reader As XmlReader = XmlReader.Create(New StringReader(xml))

	transform.Transform(reader, Nothing, results)

End Using



' Create a renderer for converting HTML to PDF

Dim Renderer As New IronPdf.ChromePdfRenderer()

' Options, headers, and footers may be set here if needed

' Render our XML as a PDF via XSLT transformation

Renderer.RenderHtmlAsPdf(results.ToString()).SaveAs("Final.pdf")
$vbLabelText   $csharpLabel

Infographic

XML to PDF

Frequently Asked Questions

What is the recommended approach to convert XML to PDF in C#?

The recommended approach is to use XSLT as a transformation template to convert XML to HTML, and then render the HTML to PDF using a .NET PDF generator like IronPDF.

Why is XSLT used in converting XML to PDF?

XSLT is used because it acts as a custom translator, converting XML from a given schema to an accurate HTML representation, which can then be rendered to PDF.

How do you install the XML to PDF Converter C# library?

You can install the XML to PDF Converter C# library by visiting the IronPdf package page on NuGet and following the installation instructions provided there.

What are the steps to convert XML to PDF using a .NET PDF generator?

The steps include: 1) Installing a .NET PDF generator library, 2) Using the Load method to import the XSLT transformer template, 3) Converting XML to HTML using the Transform method, 4) Rendering the HTML to PDF, and 5) Exporting the PDF document.

Can I download a sample project for XML to PDF conversion?

Yes, you can download a sample project showcasing how to convert XML to PDF from the XML to PDF Conversion Example link available on the webpage.

What is the role of the XslCompiledTransform class in this process?

The XslCompiledTransform class is used to load the XSLT template and perform the transformation from XML to HTML.

What are some compatible platforms for using a .NET PDF generator?

A .NET PDF generator like IronPDF is compatible with Microsoft Windows, Linux (Debian, CentOS, Ubuntu), MacOS, Docker (Windows, Linux, Azure), Azure (VPS, Webapps, Websites, Functions), and AWS.

Is there any specific styling applied during the XML to HTML transformation?

Yes, the XSLT template includes specific styling, such as centered text alignment, padding, and border styles for table elements.

Chaknith Bin
Software Engineer
Chaknith works on IronXL and IronBarcode. He has deep expertise in C# and .NET, helping improve the software and support customers. His insights from user interactions contribute to better products, documentation, and overall experience.