InfoPath - Programatically set values for the Rich Text field from SharePoint List

Scenario:

-  Populating the rich text field content from SharePoint List to the InfoPath form's Rich Text field

Problem:

-  The InfoPath form's rich text field won't recognize the contents read from SharePoint list's rich text field as valid XML. The SharePoint list's rich text field storest the raw html (malformed) which is not compatible with XML.

Solution:

-  The SharePoint list's rich text field contents needs to be parsed/converted to a well-formed HTML so that the XML can recognize without any validation errors.

-  The malformed HTML can be converted to a well-formed XML compatible using the codeplex tool HTMLAgilityPack.

Code Snippet:

// Instantiate the HtmlDocument instance from HtmlAgilityPack

HtmlDocument commentsDoc = new HtmlDocument();

commentsDoc.LoadHtml(richTextComment);

commentsDoc.OptionOutputAsXml =

true;

 

HtmlNodeCollection nodes = commentsDoc.DocumentNode.SelectNodes("//div[@class!='']");

 

if (nodes != null)

{

 

foreach (HtmlNode node in nodes)

{

node.Attributes.Remove(

"class");

}

}

nodes =

null;

commentsNode.InnerXml =

"<div xmlns='https://www.w3.org/1999/xhtml'>" + commentsDoc.DocumentNode.InnerHtml + "</div>";