本文將告訴您,如何將 Microsoft Office InfoPath 2003 表單上的 RTF
文字方塊控制項繫結到從 Web 服務傳回的 XML 項目。
如果要將 RTF 文字方塊控制項繫結到從 Web 服務傳回的 XML 項目,則該
RTF 文字方塊控制項必須包含 XHTML 內容。項目必須具有下列 XML 結構描述:
<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>
<elementname> 是從 Web 服務傳回之 XML
項目的名稱。
當 InfoPath 第一次連接到 Web 服務資料來源時,InfoPath 可以從項目中取得範例值,以自動偵測該項目是否為
XHTML 項目 。本文將告訴您,如何建立可傳回有效 XHTML 的 Web 服務,以及如何在 InfoPath 表單的 RTF 文字方塊控制項中,顯示從
Web 服務傳回的 XHTML。
建立 Web 服務
- 啟動 Microsoft Visual Studio .NET。
- 在 [檔案] 功能表中,按一下
[新增],然後按一下 [專案]。
- 在 [專案類型] 清單中,按一下 [Visual C#
專案]。在 [範本] 清單中,按一下 [ASP.NET Web
服務]。
- 在 [位置] 方塊中,輸入
http://<SERVER>/RichTextService,其中
<SERVER> 是您網頁伺服器的名稱,然後按一下
[確定]。
- 用滑鼠右鍵按一下 [Service1.asmx],然後按一下
[檢視程式碼]。
- 將下列的 Web 服務方法加入 Service1 類別:
[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;
} - 在 [建置] 功能表上,按一下
[建置方案]。
- 結束 Visual Studio .NET。
建立 InfoPath 表單
- 啟動 InfoPath。
- 在 [檔案] 功能表上,按一下
[設計表單]。
- 在 [設計表單] 工作窗格中,按一下
[從資料連線新增...]。
「資料來源安裝精靈」便會啟動。 - 安裝資料來源,如下所示:
- 按一下 [Web 服務] 做為資料來源,然後按一下
[下一步]。
- 按一下 [接收資料],然後按一下
[下一步]。
- 輸入
http://<SERVER>/RichTextService/Service1.asmx 做為
Web 服務的位置,然後按一下 [下一步]。
- 在 [選取作業] 清單中,按一下
[GetXHTMLRichText],然後按一下 [下一步]。
- 按一下 [完成]。
- 切換至 [資料來源] 工作窗格,然後展開 [dataFields]
群組。
- 展開 [GetXHTMLRichTextResponse] 群組,然後將
[GetXHTMLRichTextResult] 項目移到您的表單中。
InfoPath 會新增 RTF
文字方塊 控制項到檢視中。
試試看
- 在 [工作] 窗格下拉式清單上,按一下 [檢視]。
- 在 [檢視] 清單中,按一下
[查詢]。用滑鼠右鍵按一下 [查詢],然後按一下
[設成預設值]。
- 在 [檔案] 功能表上,指向
[預覽表單],然後按一下 [預設值]。
- 按一下 [執行查詢]。
- 在 [檢視] 功能表上,按一下
[資料項目]。
請注意表單上 RTF 文字方塊控制項中的值。Web 服務傳回的值是 Red
Text,且該值以紅色顯示。