How to programmatically get or set the XHTML code of a Rich Text field in InfoPath 2007

Applies to: InfoPath 2007

Learn how to use the InnerXml property of an XML node to get and set the XHTML code of a Rich Text field on an InfoPath form template.

ADVERTISEMENTS

Problem

You have a Rich Text Box control on an InfoPath form template and want to either get or set the XHTML code that is contained within this field.

Solution

Programmatically get or set the InnerXml property of the XML node that represents the Rich Text field.

Discussion

If you have a Rich Text Box control named rtfField on an InfoPath form template and would like to get the XHTML code for this field and store it in a string variable named xhtmlRTFField, you could use the following C# code:

XPathNavigator domNav = MainDataSource.CreateNavigator();
string xhtmlRTFField = domNav.SelectSingleNode("//my:rtfField", NamespaceManager).InnerXml;

or the following Visual Basic code:

Dim domNav As XPathNavigator = MainDataSource.CreateNavigator()
Dim xhtmlRTFField As String = domNav.SelectSingleNode("//my:rtfField", NamespaceManager).InnerXml

If you have a Rich Text Box control named rtfField on an InfoPath form template and would like to set the XHTML code for this field to "This is just a piece of red text.", you could use the following C# code:

XPathNavigator domNav = MainDataSource.CreateNavigator();
domNav.SelectSingleNode("//my:rtfField", NamespaceManager).InnerXml =
"This is just a piece of <span xmlns=\"http://www.w3.org/1999/xhtml\" style=\"color:red\">red text</span>.";

or the following Visual Basic code:

Dim domNav As XPathNavigator = MainDataSource.CreateNavigator()
domNav.SelectSingleNode("//my:rtfField", NamespaceManager).InnerXml = _
"This is just a piece of <span xmlns=""http://www.w3.org/1999/xhtml"" style=""color:red"">red text</span>."

 


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. 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.

InfoPath 2013 Cookbook: 121 Codeless Recipes for Beginners

InfoPath 2013 Cookbook 2: 121 Codeless Recipes for SharePoint 2013

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