How to get HTML tags to appear as HTML and not as plain text in a Rich Text Box

Learn how to write code to render HTML in a Rich Text Box.

Problem

You have added a Rich Text Box control to an InfoPath form and filled it with the information from the HTML file, but it does not render the tags, just prints them as plain text. You want to render the HTML.

Solution

If you want HTML tags to render as HTML and not as plain text in a Rich Text Box control on an InfoPath form, you have to add those HTML tags as child nodes to the node of the Rich Text Box or set the InnerXml property of the Rich Text Box control.

Any HTML tags that you add as child nodes must have the following namespace: http://www.w3.org/1999/xhtml.

Important: If you set the Value property of a Rich Text Box control instead of add HTML tags as child nodes or set the InnerXml property of the Rich Text Box control, the text will appear as plain text.

Discussion

If you have a Rich Text Box named rtfField and you want to add the following HTML code to the Rich Text Box:

<h2>Header Text</h2><p>This is some paragraph text.</p>

You could use the following C# code:

MainDataSource.CreateNavigator().SelectSingleNode("//my:rtfField", NamespaceManager).AppendChild("<h2 xmlns=\"http://www.w3.org/1999/xhtml\">Header Text</h2><p xmlns=\"http://www.w3.org/1999/xhtml\">This is some paragraph text.</p>");

For an example on how to use the InnerXml property to set the contents of a Rich Text box control in InfoPath, see How to programmatically get or set the XHTML code of a Rich Text field in InfoPath 2007.

 
 Subscribe for updates via RSS or Email

Related InfoPath Articles:

Copyright: This article may not be used on web sites (whether personal or otherwise), copied, disseminated, altered, printed, published, broadcasted, or reproduced in any way without an expressed written consent of S.Y.M. Wong-A-Ton. The techniques demonstrated in this article may be used within any Microsoft InfoPath project. This article is provided without any warranties. Copyright for this article is non-transferrable and remains with the author, S.Y.M. Wong-A-Ton.

InfoPath 2013 Cookbook: 121 Codeless Recipes for Beginners

InfoPath 2010 Cookbook: 101 Codeless Recipes for Beginners

InfoPath 2010 Cookbook 2: 101 Codeless Recipes for SharePoint 2010

InfoPath 2010 Cookbook 3: 101 Code Recipes for C# Developers

InfoPath 2010 Cookbook 4: 101 Code Recipes for VB Developers

InfoPath 2010 Cookbook 5: Integrating InfoPath with Excel and Excel Services