Bulleted list from XML and XSL

Bulleted list from XML and XSL

This code sample helps getting a bulleted list from XML and XSL.

See PDF output.

XML

<?xml version="1.0" encoding="utf-8" ?>
<countries>
  <country>
    <name>Sweden</name>
  </country>
  <country>
    <name>Norway</name>
  </country>
  <country>
    <name>Denmark</name>
  </country>
</countries>

XSL

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:template match="/">
    <document>
      <section pagesize="a4" startonnewpage="true">
        <xsl:apply-templates select="countries"/>
      </section>
    </document>
  </xsl:template>

  <xsl:template match="country">
    <paragraph type="NumberedItem">
      <numberfragment type="fragment" font="symbol" fontsize="10">&#xb7;</numberfragment>
      <fragment fontsize="10">
        <xsl:value-of select="name"/>
      </fragment>
    </paragraph>
  </xsl:template>

</xsl:stylesheet>

C#

// Load XML
XmlDocument xml = new XmlDocument();
xml.Load("data.xml");

// Load XSL
XslTransform xsl = new XslTransform();
xsl.Load("transform.xsl");

// Transform
XmlReader reader = xsl.Transform(xml, null);

// Write PDF
Document doc = new Document();
doc.Read(reader);

using (FileStream file = new FileStream("out.pdf", FileMode.Create, FileAccess.ReadWrite))
{
  doc.Write(file);
}