Using ASP.NET and Visual Basic .NET to Transform XML to RTF and Display in Word

Office 2003

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

Summary: Learn different ways of converting XML data to RTF. XML is a flexible way of organizing and presenting data. RTF is a text-based format that encodes formatted text, document layout, and graphics. Converting XML data to RTF allows you to create useful documents easily and quickly from a wide variety of data sources. (15 printed pages)

This article demonstrates how to generate Rich Text Format (RTF) from Extensible Markup Language (XML) by using Microsoft Visual Basic .NET. Rich Text Format is a text-based format that encodes formatted text, document layout, and graphics. It is commonly used with word processors such as Microsoft Office Word 2003. Because RTF is text-based, you can easily generate it with code. If you have XML data that you want to display in Word such as a catalog, list or mail-merge document, then transforming your XML data into an RTF stream might be an ideal solution for you. And, in fact, if you are developing a solution where you generate documents on a Web server, then generating those documents using a text-based format, such as HTML or RTF, is preferred over using server-side automation of Word.

This article provides code, with step-by-step instructions, to transform XML into RTF for display in Word by using various approaches:

Save the RTF to a file and open it in Word.

Transfer the RTF to Word by using the Clipboard.

Stream the RTF to Word hosted in Microsoft Internet Explorer from an ASP.NET Web Application using a simple and then a more complex transformation.

Note

While the RTF specification discussed in this article is a method commonly used for creating documents in Microsoft Office 2002 and later versions, for Microsoft Office 2003 Editions, the preferred method for creating Word documents from XML data is Microsoft WordML in conjunction with XSLT.

Click To File to save the transformed XML to a file (Dictionary.rtf). You can open the RTF file in Word to examine the results of the transformation.

Click To Clipboard to copy the transformed XML to the Clipboard. You can then paste the Clipboard contents into a new or existing Word document to see the results. For example, start Word, click into the document to place the cursor, click Edit, and then click Paste.

Visual Basic .NET allows you to transform DataSet objects. DataSet objects generally contain a subset of the data from the data source, similar to an in-memory relational database.

This procedure shows you how you can take related data from the sample Northwind database and transform it to RTF. Two different transformations are demonstrated: a simple RTF document that lists Customer contact information and a somewhat more complex RTF document that displays order information for customers in a mail merge-type format.

To transform a DataSet into RTF

Create a Visual Basic ASP.NET Web Application and save it to http://localhost/RTFDemo. WebForm1 is created for you.

Received a Message Such as "Login failed for user 'MachineName\ASPNET"

If you receive a message such as "Login failed for user MachineName\ASPNET" when you click either of these buttons, the most likely problem is that the account \ASPNET does not have the proper permissions in SQL Server, and therefore it cannot access the database. To correct the problem, do the following:

Start SQL Server Enterprise Manager.

Expand the different levels to display (local) (Windows NT).

Expand the Security folder.

Click the Logins icon, right-click, and select the New Login option.

In the Name box, type aspnet. Ensure that Windows Authentication is selected.

In the Domain drop-down box, select the local domain for your computer. Leave the default Security access as Grant access.

Under Defaults, in the Database drop-down box, select Northwind. Leave English as the default language.

Click OK.

Retry either button.

To save the RTF to a file and redirect to the saved file

As written, the sample code in the section Transform a DataSet Into RTF streams RTF directly to the browser. Alternatively, you can save the RTF to a file and redirect to the saved file. To do this, replace these lines of code in the sample

Storing the RTF to a file in this manner allows you to easily examine the structure of the RTF in the file by using a text editor, such as Notepad. Storing the RTF to a file can be a helpful troubleshooting technique if the XSL transformation does not produce the results you expect.

Be aware of How You Present White Space and Carriage Returns

When transforming to RTF, be aware of how you present white space and carriage returns in your style sheet because that can affect how Word interprets your RTF. Both code samples in this section use the <xsl:text> element because it forces any white space information in it to be retained.

Ensure That Your XML Is Transformed to Text

Use <xsl:output method="text"> in your style sheet to make sure that your XML is transformed to text (rather than XML, which is the default output method). If you do not specify text as the output method, XML processing instructions may be added to the file. This can prevent Word from correctly interpreting the text as RTF.

In this article, we looked at different ways to convert XML data into RTF. For example, we saw how you can transform custom XML into RTF either saved to a file or copied to the Clipboard. We also looked at how you can use Visual Basic .NET to transform datasets easily. Since RTF is a text-based format, it easily lends itself to creation in code. In addition, XML has become the de facto standard for presenting data. For this reason, it is well worth the time to learn these ways to create RTF documents, where applicable, in your own applications.