For a Rich Text Box control to bind to an XML element that is returned from a Web service, the Rich Text Box control must contain XHTML content. The element must have the following XML schema:
<xsd:element name="[elementname]">
<xsd:complexType mixed="true">
<xsd:sequence>
<xsd:any namespace="http://www.w3.org/1999/xhtml" processContents="lax"
minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
InfoPath can automatically detect if an element is an XHTML element by querying the element for a sample value when InfoPath connects to the Web service data source for the first time. This article describes how to create a Web service that returns valid XHTML. This article also describes how to display the XHTML that is returned from the Web service in a Rich Text Box control on an InfoPath form.
Create the Web service
- Start Microsoft Visual Studio .NET.
- On the File menu, click New, and then click Project.
- In the Project Types list, click Visual C# Projects. In the Templates list, click ASP.NET Web Service.
- In the Location box, type http://<SERVER>/RichTextService where <SERVER> is the name of your Web server, and then click OK.
- Right-click Service1.asmx, and then click View Code.
- Add the following Web Service method to the Service1 class:
[WebMethod] public System.Xml.XmlNode GetXHTMLRichText() { //Create a temporary XmlDocument object to generate nodes. System.Xml.XmlDocument tempDocument = new System.Xml.XmlDocument(); //Create a wrapper node for the data. This is necessary so InfoPath //correctly detects the XHTML content System.Xml.XmlElement theNode = (System.Xml.XmlElement)tempDocument.CreateNode( System.Xml.XmlNodeType.Element, "theNode", "http://somearbitrarynamespace/" ); //Create a "font" element in the xhtml namespace. System.Xml.XmlElement theFontNode = (System.Xml.XmlElement)tempDocument.CreateNode( System.Xml.XmlNodeType.Element, "font", "http://www.w3.org/1999/xhtml" ); theFontNode.InnerText= "Red Text"; //Add a color attribute. System.Xml.XmlAttribute colorAttribute = tempDocument.CreateAttribute( "color" ); colorAttribute.Value = "#ff0000"; theFontNode.Attributes.Append( colorAttribute ); //Append the font node to the wrapper node theNode.AppendChild( theFontNode ); //Return the wrapper element. return theNode; }
- On the Build menu, click Build Solution.
- Exit Visual Studio .NET.
Create the InfoPath form
In InfoPath 2003
- Start InfoPath 2003.
- On the File menu, click Design a Form.
- In the Design a Form task pane, click New from Data Connection....
The Data Source Setup Wizard starts. - Setup the data source as follows:
- Click Web Service for the data source, and then click Next.
- Click Receive data, and then click Next.
- Type http://<SERVER>/RichTextService/Service1.asmx for the location of the Web service, and then click Next.
- In the Select an operation list, click GetXHTMLRichText, and then click Next.
- Click Finish.
- Switch to the Data Source task pane, and then expand the dataFields group.
- Expand the GetXHTMLRichTextResponse group, and then move the GetXHTMLRichTextResult element to your form.
InfoPath adds a Rich Text Box control to the view.
In InfoPath 2007
- Start InfoPath 2007.
- In the left pane of the Getting Started dialog box, click Design a Form Template.
In the Design a Form Template window, click Blank, and then click OK. - On the Tools menu, click Data Connections.
- In the Data Connections window, click Add.
The Data Source Setup Wizard starts. - Set up the data source as follows:
- Click to select Create a new connection to, click to select Receive data, and then click Next.
- Click to select Web Service for the data source, and then click Next.
- Type http://<SERVER>/RichTextService/Service1.asmx for the location of the Web service, and then click Next.
- In the Select an operation list, click GetXHTMLRichText, and then click Next.
- Click Finish.
- Switch to the Data Source task pane, and then expand the dataFields group.
- Expand the GetXHTMLRichTextResponse group, and then move the GetXHTMLRichTextResult element to the form.
InfoPath adds a Rich Text Box control to the view.
Try it out
- On the Task pane drop-down list, click Views.
- In the Views list, click Query. Right-click Query, and then click Set as Default.
- On the File menu, point to Preview Form, and then click Default.
- Click Run Query.
- On the View menu, click Data Entry.
Notice the value that is in the Rich Text Box control on the form. The value that is returned by the Web service is Red Text, and the value is formatted in red.