RE: Transfer text from Rich Text Box

Skip to first unread message


Mar 8, 2006, 10:54:58 AM3/8/06
It seems that InfoPath will not allow me to do this in code. I think I might
have to write a webservice to transfer the text.

"S.Y.M. Wong-A-Ton" wrote:

> The contents of a rich text box is stored as XHTML. To preserve line breaks,
> which are essentially achieved through the use of <DIV> elements within the
> contents of the rich text box, you would have to copy the entire XML content
> of the node pertaining to the first rich text box, create a new node, set the
> XML content of the newly created node to the content copied from the first
> rich text box, and then replace the node pertaining to the second rich text
> box with the new node using replaceChild().
> ---
> S.Y.M. Wong-A-Ton
> "Andrew" wrote:
> > Does anyone know of a way to transfer text from one rich text box to another
> > and being able to keep the return lines in there.
> >
> > Thanks,
> > Andrew

S.Y.M. Wong-A-Ton

Mar 8, 2006, 12:19:27 PM3/8/06
If your scenario includes simply copying text from one RTF to another on one
InfoPath form, then this is doable through code without using a web service.
I don't understand why you would need a web service. Is there a database
(that you haven't mentioned) somewhere in the equation?


Mar 8, 2006, 2:02:43 PM3/8/06
Nope no database. I have code behind in c#. I created a test form that just
has a Rich TextBox a and b. And when the user submits the form I would like
to transfer the Rich Text from A to B and beable to keep the line breaks.

I've tried to do something like this
Node =

I do understand why that won't work but I'm still new to XML.

and If I do this ...
thisXDocument.DOM.selectSingleNode("/my:myFields/my:b").text =

Then it copies the text over without keeping the line breaks.

Thanks for any help!
- Andrew

S.Y.M. Wong-A-Ton

Mar 9, 2006, 4:03:27 AM3/9/06
If your RTF-a contains line breaks and you put a breakpoint on e.g.

string RTF-a = thisXDocument.DOM.selectSingleNode("/my:myFields/my:a").xml;

you will see a bunch of <DIV> elements contained in <my:a>. What worked for
me was to build a loop over the childNodes contained in <my:a> and then
compose a string from these childNodes. I haven't yet found a "nicer" way to
copy the XML inside a node. You can do a check on


before starting the loop over the childNodes. Then you must retrieve the
nodeName, nodeType, and namespaceURI of <my:b> (these are all properties of a
node) and use them to create the new XML for this node as a string that
contains the inner XML copied from <my:a>.

You can then use thisXDocument.CreateDOM() to instantiate a new XML node
(we'll call this newChild), and then newChild.loadXML() to load the XML
string for the new <my:b> into this new XML node.

Then you retrieve the old <my:b> (let's call it oldChild) and with a
replaceChild(), you replace it with the new XML node for <my:b> like this

oldChild.parentNode.replaceChild(newChild.documentElement, oldChild);

Hope this helps. If you're still having difficulty getting this to work,
I'll convert your scenario to a "recipe" (=solution) and post it on my
website this weekend.


Mar 9, 2006, 9:15:28 AM3/9/06
Thank you for all of your help!! I got it working this morning. The only
difference I had to make was I also needed to keep everything that is in the
OldChile. So that the new text would be first then the old test second.

Oh you mentioned that you have a website, may I have the web address to
check it out?

Thank you again!!

S.Y.M. Wong-A-Ton

Mar 9, 2006, 9:56:27 AM3/9/06
Glad to hear that you got it to work! And since you did, I won't be posting
the solution on my website. But you're free to check out the website at Gustavo Hernandez

Apr 16, 2007, 5:44:03 PM4/16/07
Hi My name is Gustavo from Monterrey mexico

I have the same problem, but i have one Rich Text field with tables, text in
bold, text in different size, etc...and i would like to copy the values from
one field to another field,
do you think this code could be used also to pass that kind of information?

S.Y.M. Wong-A-Ton

Apr 17, 2007, 2:24:01 AM4/17/07
If the other field is a richt text field, then yes, the same principles
should apply. If you're using InfoPath 2007, you might even get away with
using the InnerXml property to transfer the entire contents in one step; I
haven't tried it out, though. I would say: Just give it a go and see if it
S.Y.M. Wong-A-Ton

Gustavo Hernandez

Apr 17, 2007, 8:56:04 AM4/17/07
Thanks for your quick answer.

And yes both fields are rich text. I'm trying to use your suggestion about
how to transfer text from one field to another, but i'm having problems when
i want to do the replace child instruction. My field "B" does not have any
How can i create a new child for my field "B" where i want to pass the rich
text value from my field "A" ?, i mean i can do a loop for my field "A" and
get the XML data, but i don't understand how to pass every XML data for each
child of my field "A" to my field "B".

S.Y.M. Wong-A-Ton

Apr 17, 2007, 9:04:02 PM4/17/07
You can do it on a number of ways. Which version of InfoPath are you using?

Gustavo Hernandez

Apr 23, 2007, 12:26:02 PM4/23/07
I'm using InfoPath 2003. Finally i found a way to do it. A little different
than your suggestions, but following your instructions. One thing different
that i did was copy the xml of my field A into a string variable and only
replace the name of the child for the field2 and then do a replace child from
parent node of both fields.

IXMLDOMNode node1 =

if (node1 != null)

IXMLDOMDocument newchild = doc.CreateDOM();
childtext = node1.xml.ToString();
string newtxtofnode2= childtext.Replace("NameOfChild_1",

oldchild =

oldchild.parentNode.replaceChild(newchild.documentElement, oldchild);

And it works !!

Thanks for your help !

Nov 5, 2012, 1:47:33 PM11/5/12
I was able to get this working with a single line of VB code:

Me.MainDataSource.CreateNavigator.SelectSingleNode("/my:b",NamespaceManager).InnerXML = Me.MainDataSource.CreateNavigator.SelectSingleNode("/my:a",NamespaceManager).InnerXML

This is in InfoPath2010, creating a 2007-compatible form.

Jul 21, 2014, 7:10:08 AM7/21/14
Hi Andrew,

Could you explain where we have to write this code in office 365 - Info path 2013.

Reply all
Reply to author
0 new messages