filling Rich Text Box with XHTML - InfoPath Dev
in

InfoPath Dev

filling Rich Text Box with XHTML

Last post 10-24-2008 12:06 AM by Jimmy. 3 replies.
Page 1 of 1 (4 items)
Sort Posts: Previous Next
  • 10-22-2008 02:29 AM

    • n0e2008
    • Not Ranked
      Male
    • Joined on 10-22-2008
    • Poland
    • Posts 3

    filling Rich Text Box with XHTML

     Hi

    I've got a little problem concerning Rich Text Box (in IP2007), which I want to fill programmatically with XHTML data. I know I need to append XHTML nodes as child nodes of a Rich Text Box, so I'm doing something like this:

                                    if (localNode.MoveToChild("Example", myNamespace))
                                    {
                                       using (XmlWriter rtfWriter = localNode.AppendChild())
                                        {
                                            rtfWriter.WriteStartElement("div", "http://www.w3.org/1999/xhtml");
                                            rtfWriter.WriteString(memberNode.InnerXml);
                                            rtfWriter.WriteEndElement();
                                            rtfWriter.WriteStartElement("div", "http://www.w3.org/1999/xhtml");
                                            rtfWriter.WriteString("linia2");
                                            rtfWriter.WriteEndElement();
                                            rtfWriter.Close();
                                        }

     After appending these two div nodes I should be seeing two strings in new lines in the Rich Text Box. Unfortunately this is not happening, I see them inline, without any formatting, like it was just plain text.

     I've tried to test aforementioned approach in a new infopath template, and surprisingly everything was fine (button click filled the richtextbox with xhtml) BUT after saving the template and opening it again, it was filling the box with plain text instead of xhtml! After searching a bit I've discovered there's a problem with namespace of the parent node ('localNode' in this example). The 'myNamespace' in example above is defined as:

    string myNamespace = NamespaceManager.LookupNamespace("my");

    In the newly created form there wasn't any namespace added to the rich text box node (so it looked like <Example>) but after saving the default "my" namespace was added to the control's node (so it looked like <my:Example xmlns:my=...>) and the xhtml content was rendered as plain text.

    I know the namespace nodes are defined upon element's creation and afterwards they are read-only, so what can I do? Any ideas?

    Outerxml of my root node is (unneeded parts cut):

     <?xml version="1.0"?><?mso-infoPathSolution name="urn:schemas-microsoft-com:office:infopath:DocView1:-myXSD-2008-10-08T08-15-26" solutionVersion="1.0.0.307" productVersion="12.0.0" PIVersion="1.0.0.0" ?><?mso-application progid="InfoPath.Document" versionProgid="InfoPath.Document.2"?>

    <my:MainGroup xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2008-10-08T08:15:26" xmlns:xd="http://schemas.microsoft.com/office/infopath/2003" xml:lang="en-us">

     <my:Example></my:Example>

     

  • 10-22-2008 02:49 AM In reply to

    • n0e2008
    • Not Ranked
      Male
    • Joined on 10-22-2008
    • Poland
    • Posts 3

    Re: filling Rich Text Box with XHTML

     oh and outerxml of my:Example after appending xhtml child nodes looks like this:

    <my:Example xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2008-10-08T08:15:26">
    <xhtml:div xmlns:xhtml="http://www.w3.org/1999/xhtml">this is an example</xhtml:div>
    <xhtml:div xmlns:xhtml="http://www.w3.org/1999/xhtml">linia2</xhtml:div></my:Example>

    this theoretically should work, but it doesn't.

     

  • 10-22-2008 05:49 AM In reply to

    • n0e2008
    • Not Ranked
      Male
    • Joined on 10-22-2008
    • Poland
    • Posts 3

    Re: filling Rich Text Box with XHTML

    ok, i was wrong. it's not the namespace BUT something strange happens after saving the form, exiting designer and opening the form again. before the form is closed everything looks fine, after reopening - no formatting. xml of the form looks exactly the same in both cases.

    is it a bug?

  • 10-24-2008 12:06 AM In reply to

    Re: filling Rich Text Box with XHTML

    After a bit of experimenting I think I've found a solution!

    First, save your form as its source files (from the File menu).  Then close infopath and browse to the directory where you saved the files.

    Open up the main schema file (probably myschema.xsd) and add this text to the end of the <xsd:schema> node at the top, along with the other namespace declarations:

        xmlns:xhtml="http://www.w3.org/1999/xhtml"

    Then save the file, right-click manifest.xsf and choose Design.  Use File->Save As... to re-save your template as an .xsn.

    In your code you will need to explicitly use a blank namespace prefix, so instead of:

        rtfWriter.WriteStartElement("div", "http://www.w3.org/1999/xhtml");

    use

        rtfWriter.WriteStartElement("", "div", "http://www.w3.org/1999/xhtml"); 

    I hope that works.

    Good luck,
    Jimmy

    P.S. Just thought I would point out that you don't need to use rtfWriter.Close(); when the code is inside a using() block.  The using() block takes care of that for you.
                                       

    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
Page 1 of 1 (4 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.