文章編號: 826996 - 上次校閱: 2006年4月11日 - 版次: 2.5

如何將 RTF 文字方塊控制項繫結到從 InfoPath 2003 與 Visual Studio .NET (2003) 之 Web 服務傳回的項目

系統提示本文適用於您使用的作業系統之外的作業系統。與您不相關的文章內容已停用。

在此頁中

全部展開 | 全部摺疊

結論

本文將告訴您,如何將 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 服務

  1. 啟動 Microsoft Visual Studio .NET。
  2. [檔案] 功能表中,按一下 [新增],然後按一下 [專案]
  3. [專案類型] 清單中,按一下 [Visual C# 專案]。在 [範本] 清單中,按一下 [ASP.NET Web 服務]
  4. [位置] 方塊中,輸入 http://<SERVER>/RichTextService,其中 <SERVER> 是您網頁伺服器的名稱,然後按一下 [確定]
  5. 用滑鼠右鍵按一下 [Service1.asmx],然後按一下 [檢視程式碼]
  6. 將下列的 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;
    	}
  7. [建置] 功能表上,按一下 [建置方案]
  8. 結束 Visual Studio .NET。

建立 InfoPath 表單

  1. 啟動 InfoPath。
  2. [檔案] 功能表上,按一下 [設計表單]
  3. 在 [設計表單] 工作窗格中,按一下 [從資料連線新增...]

    「資料來源安裝精靈」便會啟動。
  4. 安裝資料來源,如下所示:
    1. 按一下 [Web 服務] 做為資料來源,然後按一下 [下一步]
    2. 按一下 [接收資料],然後按一下 [下一步]
    3. 輸入 http://<SERVER>/RichTextService/Service1.asmx 做為 Web 服務的位置,然後按一下 [下一步]
    4. [選取作業] 清單中,按一下 [GetXHTMLRichText],然後按一下 [下一步]
    5. 按一下 [完成]
  5. 切換至 [資料來源] 工作窗格,然後展開 [dataFields] 群組。
  6. 展開 [GetXHTMLRichTextResponse] 群組,然後將 [GetXHTMLRichTextResult] 項目移到您的表單中。

    InfoPath 會新增 RTF 文字方塊 控制項到檢視中。

試試看

  1. 在 [工作] 窗格下拉式清單上,按一下 [檢視]
  2. [檢視] 清單中,按一下 [查詢]。用滑鼠右鍵按一下 [查詢],然後按一下 [設成預設值]
  3. [檔案] 功能表上,指向 [預覽表單],然後按一下 [預設值]
  4. 按一下 [執行查詢]
  5. [檢視] 功能表上,按一下 [資料項目]

    請注意表單上 RTF 文字方塊控制項中的值。Web 服務傳回的值是 Red Text,且該值以紅色顯示。

這篇文章中的資訊適用於:
  • Microsoft Office InfoPath 2003
  • Microsoft Visual Studio .NET 2003 Enterprise Developer
  • Microsoft Office InfoPath 2003, Service Pack 1 (SP1)
關鍵字:?
kbhowtomaster kbwebservices kbhowto KB826996
Microsoft及(或)其供應商不就任何在本伺服器上發表的文字資料及其相關圖表資訊的恰當性作任何承諾。所有文字資料及其相關圖表均以「現狀」供應,不負任何擔保責任。Microsoft及(或)其供應商謹此聲明,不負任何對與此資訊有關之擔保責任,包括關於適售性、適用於某一特定用途、權利或不侵權的明示或默示擔保責任。Microsoft及(或)其供應商無論如何不對因或與使用本伺服器上資訊或與資訊的實行有關而引起的契約、過失或其他侵權行為之訴訟中的特別的、間接的、衍生性的損害或任何因使用而喪失所導致的之損害、資料或利潤負任何責任。