February 28, 2021
Hot Topics:

Take the Pain out of Creating Word Documents by Using C# and XML

  • By Chris Bennett
  • Send Email »
  • More Articles »

This modification inserts the Title into this position in the document. Next, you'll dig into the region and groups. A couple of lines down in the XSLT, after the line above, you should locate a line as follows:


After this line, add the following XSLT statement to loop through part of the document for each region.

<xsl:for-each select="doc/region">

Again, you will need to close the XSLT statement towards the end of the document. The closing statement should appear after the </w:tbl>. Now that you are at this point, it would be good to explain a couple of the tags in the XML document. If you are familiar with HTML table tags, there are some similarities, such as <w:tbl> for table, <w:tr> for table row, and <w:tc> for table cell. Next, you need to put in the tags to populate the region values within the loop. Search for the text "Region". Replace this line with the line below:

<w:t><xsl:value-of select="name"/> --
     <xsl:value-of select="salesdiff"/></w:t>

This line places the name and salesdiff values for this region. The group below region is performed using the same process of the <xsl:for-each> and <xsl:value-of> statements.

Transformation Method

Next, build the code to create the XML data and transform it using the XSLT you just created. Listed below is the method that uses all of this and creates the Word document.

private void TransformToDoc(XmlDocument doc)
   // XslTransform class for loading an XSLT file.
   XslCompiledTransform _transform;

   // String: Path to XSLT file.
   string _xslPath;

   // Get paths for XSLT file
   _xslPath = Server.MapPath("Template.xslt");

   string filename = "Document - " +
      DateTime.Now.ToString("MM-dd-yyyy HHmmss") + ".doc";
   Page.Response.Buffer = true;
      "attachment;filename=" + filename);
   Page.Response.ContentType = "application/x-msword";
      Page.Response.Charset = null;

   // Transform _transform = new XslCompiledTransform();
   _transform.Transform(doc, null, Page.Response.OutputStream);

By using the input XMLDocument, this method transforms the XML into Word compatible XML using the XSLT you created above. This method also populates the HTTP header and Content Type information so the web browser will prompt the user to open the document with Microsoft Word. In the event that you want to save the XML document directly, it is fairly simple to remove the Page statements and change the _transform.Transform call to output to a file instead of the OutputStream.


At this point, you have created a very simple process to convert a Word document to an XML file, then to an XSLT file that you can use to convert XML data to a format readable by Word. This process is compatible with both Word 2003 and 2007 and works especially well with web applications; with the proper header values, Word will automatically open the file transparently to the user. Its important to note that, because this approach uses XML, it can produce files that can become quite large. However, after creating the XSLT, this is quite simple and works very well for reports without the need for third-party libraries.

Download the Code

You can download the code that accompanies the article here.

About the Author

Chris Bennett is a manager with Crowe Horwath LLP in the Indianapolis office. He can be reached at chris.bennett@crowehorwath.com.

Page 2 of 2

This article was originally published on January 23, 2009

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date