How to programmatically get or set the XHTML code of a Rich Text field in 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.
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:
