文章編號: 826998 - 上次校閱: 2004年4月13日 - 版次: 1.0

PRB:嘗試以程式設計方式設定 XML 節點的文字值時,收到了錯誤訊息

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

在此頁中

全部展開 | 全部摺疊

徵狀

當您嘗試以程式設計方式設定 XML 節點的文字值時,可能會收到下列錯誤訊息:

發生執行階段錯誤。
您要偵錯嗎?

發生錯誤如下:
'#PCDATA' 在 nil 內容中。

發生的原因

當您嘗試以程式設計方式為含有 xsi:nil="true" 屬性的 XML 節點設定文字值時,就會產生這個錯誤。當此屬性是設定為 true 時,XML 節點的任何文字值都會產生無效的 XML。因此,Microsoft Office InfoPath 不會接受該值,並且您會收到錯誤訊息。

其他可行方案

如果要解決這個問題,請新增能夠檢查 xsi:nil="true" 屬性的程式碼,然後在程式碼設定節點的文字值之前,在執行階段中移除屬性 (如果找到屬性)。例如,將下列程式碼:
{
	// Receive a reference to the element to be filled.
	var objDataElement = XDocument.DOM.selectSingleNode("/my:myFields/my:field1");
		//Set the value of the element.
	objDataElement.text = "10.0";
}
取代為:
{
	// Receive a reference to the element to be filled.
	var objDataElement = XDocument.DOM.selectSingleNode("/my:myFields/my:field1");
		//Determine whether the xsi:nil attribute is set for this
	//element. If so, remove the xsi:nil attributes so that
	//the value can be set.
	if (objDataElement.getAttribute("xsi:nil"))
          objDataElement.removeAttribute("xsi:nil");
		//Set the value of the element.
	objDataElement.text = "10.0";
}

其他相關資訊

重現問題的步驟

  1. 啟動 Microsoft Office InfoPath。
  2. 設計新的空白表單。
  3. 在工作窗格中,按一下 [控制項]。確認已經選取 [自動建立資料來源] 核取方塊。
  4. TextBox 控制項新增至表單,再將 Button 控制項新增至表單。
  5. 在工作窗格中,按一下 [資料來源]
  6. [資料來源] 清單中,用滑鼠右鍵按一下 [field1],再按 [內容]

    隨即顯示 [欄位或群組內容] 對話方塊。
  7. 將欄位的 [資料類型] 內容設定為 [Decimal (double)],然後按一下 [確定]
  8. 依照下列步驟執行,為按鈕的 OnClick 事件新增指令碼:
    1. 用滑鼠右鍵按一下按鈕,然後按一下 [按鈕內容]

      隨即顯示 [按鈕內容] 對話方塊。
    2. 按一下 [Microsoft Script Editor] 按鈕。

      隨即顯示 [Microsoft Script Editor]。
    3. 在按鈕的 OnClick 事件中,插入下列程式碼:
      {
      	// Receive a reference to the element to be filled.
      	var objDataElement = XDocument.DOM.selectSingleNode("/my:myFields/my:field1");
      		//Set the value of the element.
      	objDataElement.text = "10.0";
      }
    4. 儲存您的指令碼,然後關閉 [Microsoft Script Editor]。
  9. 預覽表單,並按一下按鈕。

    您會收到本文<徵狀>一節中所提到的錯誤訊息。
  10. 按一下 [否],再按 [確定],關閉錯誤訊息。 關閉 [預覽] 視窗。
  11. 依照下列步驟執行,修改按鈕的 OnClick 事件:
    1. 用滑鼠右鍵按一下按鈕,然後按一下 [按鈕內容]

      隨即顯示 [按鈕內容] 對話方塊。
    2. 按一下 [Microsoft Script Editor] 按鈕。

      隨即顯示 [Microsoft Script Editor]。
    3. 依照下列步驟執行,修改按鈕的 OnClick 事件程式碼:
      {
      	// Receive a reference to the element to be filled.
      	var objDataElement = XDocument.DOM.selectSingleNode("/my:myFields/my:field1");
      		//Determine whether the xsi:nil attribute is set for this
      	//element. If so, remove the xsi:nil attributes so that
      	//the value can be set.
      	if (objDataElement.getAttribute("xsi:nil"))
                objDataElement.removeAttribute("xsi:nil");
      		//Set the value of the element.
      	objDataElement.text = "10.0";
      }
  12. 再次預覽表單,並按一下按鈕。

    將不會傳回任何錯誤訊息。 TextBox 控制項會顯示數值 10.0

?考

如需有關如何偵錯指令碼的詳細資訊,請按一下下面的文件編號,檢視「Microsoft 知識庫」中的文件:
827002? (http://support.microsoft.com/kb/827002/ ) HOW TO:Debug Script for a Microsoft Office InfoPath 2003 Form

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