Creating MS Word Document using C#, XML and XSLT

This simple program demostrate how to create well formatted MS Word documents using C#, XML and XSLT. Using XSLT to create Word document requires the knowledge of RTF key words. RTF specification is available in MSDN site.

1. Open Visual Studio .NET and select File --> New --> Project.

2. Select Visual C# project as the Project Type and Windows Application as the Template. For the project name, specify 'CShartXSLT'; for the path specify the location where you want the project to be created. Click OK to create the new project.

3. Add an XML file named "Employee.xml" in \Debug\Bin directory (other wise you have to specify the path in the program). The structure of the file will be like,

<?xml version="1.0" encoding="utf-8" ?> 
<Employee> 
  <Record> 
    <EmpID>E1</EmpID> 
    <EmpName>Sudipta</EmpName> 
    <EmpAge>29</EmpAge> 
    <EmpSex>M</EmpSex> 
    <EmpAddress>Kolkata</EmpAddress> 
    <Department> 
      <DeptID>D1</DeptID> 
      <EmpID>E1</EmpID> 
      <DeptName>Sales</DeptName> 
    </Department> 
  </Record> 
  <Record> 
    <EmpID>E2</EmpID> 
    <EmpName>Chiranjib</EmpName> 
    <EmpAge>26</EmpAge> 
    <EmpSex>M</EmpSex> 
    <EmpAddress>Kolkata</EmpAddress> 
    <Department> 
      <DeptID>D1</DeptID> 
      <EmpID>E2</EmpID> 
      <DeptName>Sales</DeptName> 
    </Department> 
  </Record> 
  <Record> 
    <EmpID>E3</EmpID> 
    <EmpName>Nilanjan</EmpName> 
    <EmpAge>29</EmpAge> 
    <EmpSex>M</EmpSex> 
    <EmpAddress>Kolkata</EmpAddress> 
    <Department> 
      <DeptID>D2</DeptID> 
      <EmpID>E3</EmpID> 
      <DeptName>Finance</DeptName> 
    </Department> 
  </Record> 
  <Record> 
    <EmpID>E4</EmpID> 
    <EmpName>Chayan</EmpName> 
    <EmpAge>30</EmpAge> 
    <EmpSex>M</EmpSex> 
    <EmpAddress>Kolkata</EmpAddress> 
    <Department> 
      <DeptID>D3</DeptID> 
      <EmpID>E4</EmpID> 
      <DeptName>Human Resource</DeptName> 
    </Department> 
  </Record> 
  <Record> 
    <EmpID>E5</EmpID> 
    <EmpName>Biplab</EmpName> 
    <EmpAge>31</EmpAge> 
    <EmpSex>M</EmpSex> 
    <EmpAddress>Kolkata</EmpAddress> 
    <Department> 
      <DeptID>D4</DeptID> 
      <EmpID>E5</EmpID> 
      <DeptName>Administration</DeptName> 
    </Department> 
  </Record> 
</Employee>

4. Add an XSLT file named "Employee.xslt" in \Debug\Bin directory (other wise you have to specify the path in the program). The structure of the file will be like,

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:dt="urn:schemas-microsoft-com:datatypes" xmlns:user="urn:my-scripts"> 
  <xsl:output method="text" /> 
  <xsl:template match="Employee"> 
    <xsl:text>{\rtf1\fs22</xsl:text> 
    <!--Print the Header Row--> 
    <xsl:text>\trowd\cellx2500\cellx3000\cellx3800\cellx6800\cellx9144\intbl\keepn\ql\b Name\cell Age\cell Sex\cell Address\cell Department\cell</xsl:text> 
    <xsl:text>\b0\ql0\intbl\row</xsl:text> 
    <!--Print Employee Records--> 
    <xsl:apply-templates select="Record" /> 
    <xsl:text>}</xsl:text> 
  </xsl:template> 
  <xsl:template match="Record"> 
    <xsl:text>\trowd\cellx2500\cellx3000\cellx3800\cellx6800\cellx9144\intbl\keepn\ql </xsl:text> 
    <xsl:value-of select="EmpName" /> 
    <xsl:text>\cell </xsl:text> 
    <xsl:value-of select="EmpAge" /> 
    <xsl:text>\cell </xsl:text> 
    <xsl:if test="EmpSex='M'"> 
      <xsl:text>Male</xsl:text> 
      <xsl:text>\cell </xsl:text> 
    </xsl:if> 
    <xsl:if test="EmpSex='F'"> 
      <xsl:text>Female</xsl:text> 
      <xsl:text>\cell </xsl:text> 
    </xsl:if> 
    <xsl:value-of select="EmpAddress" /> 
    <xsl:text>\cell </xsl:text> 
    <!--Print Employee Department--> 
    <xsl:apply-templates select="Department" /> 
    <xsl:text>\cell</xsl:text> 
    <xsl:text>\intbl\row</xsl:text> 
  </xsl:template> 
  <xsl:template match="Department"> 
    <xsl:value-of select="DeptName" /> 
  </xsl:template> 
</xsl:stylesheet> 

5. In Form1.cs, add the following references

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Xsl;
using System.Xml.XPath;
using System.IO;

6. In Form1.cs, add a button and write the following code

private void button1_Click(object sender, EventArgs e)
{
    DataSet ds;
    XmlDataDocument xmlDoc;
    XslCompiledTransform xslTran;
    XmlElement root;
    XPathNavigator nav;
    XmlTextWriter writer;
    try
    {
        //Create the DataSet from the XML file
        ds = new DataSet();
        ds.ReadXml("Employee.xml");
        //Create the XML from the DataSet
        xmlDoc = new XmlDataDocument(ds);
        //Load the XSLT for Transformation
        xslTran = new XslCompiledTransform();
        xslTran.Load("Employee.xslt");
        //Determine the Root object in the XML
        root = xmlDoc.DocumentElement;
        //Create the XPath Navigator to navigate throuth the XML
        nav = root.CreateNavigator();
        //First delete the RTF, if already exist
        if (File.Exists("Employee.rtf"))
        {
            File.Delete("Employee.rtf");
        }
        //Create the RTF by Transforming the XML and XSLT
        writer = new XmlTextWriter("Employee.rtf", System.Text.Encoding.Default);
        xslTran.Transform(nav, writer);
        //Close the Writer after Transformation
        writer.Close();
        //Release all objects
        writer = null;
        nav = null;
        root = null;
        xmlDoc = null;
        ds = null;
        MessageBox.Show("Document created successfully.....");
    }
    catch (Exception ex)
    {
        writer = null;
        nav = null;
        root = null;
        xmlDoc = null;
        ds = null;
        MessageBox.Show(ex.StackTrace);
    }
}

7. Compile and run the program.


Similar Articles