共用方式為


使用 InfoPath 2003 物件模型處理 MSXML5 和 System.Xml

搭配 InfoPath 2003 物件模型運作的表單範本專案會使用 Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office 在內部搭配 XML 一起運作。在 Managed 程式碼中,使用 .NET Framework 類別庫中的 System.Xml 命名空間所提供的 XML 支援,通常會比較容易。MSXML 與 System.Xml 都無法以原始格式交換物件,因此每當您需要在 InfoPath 與其他 Managed 程式碼之間傳遞 XML 資料時,都需要轉換 XML 資料。您可以使用本主題中的技術,將 System.Xml 物件的 XML 資料換成 InfoPath 的表單程式碼。

若要在使用 InfoPath 2003 物件模型的 Managed 程式碼專案中使用 System.Xml 命名空間的成員,您必須在 Microsoft Visual Studio Tools for Applications (VSTA) 或 Visual Studio 中,於 [加入參考] 對話方塊的 [.NET] 索引標籤上,新增 [System.Xml] 的參照。

附註

  • 若要檢視有關 MSXML 5.0 的參照資訊,請開啟 InfoPath,然後按一下 [Microsoft Office InfoPath 說明],再按一下 [搜尋],最後按一下 [InfoPath 2007 開發人員說明]。

  • Microsoft.Office.Interop.InfoPath.SemiTrust 命名空間所包裝的 MSXML 5.0 物件模型成員,無法指定在 Managed 程式碼表單範本的表單程式碼中的委派。

  • 如果您更新表單範本的程式碼來使用 Microsoft.Office.InfoPath 命名空間成員所提供的物件模型,則會以原始格式使用 System.Xml。但是,在進行這項處理時,您必須手動轉換所有程式碼,才能使用新的物件模型。若要轉換表單範本來使用新的物件模型,請在 [表單選項] 對話方塊上,按一下 [程式設計] 類別中的 [升級 OM]。

從 System.Xml 載入整個 XML 文件物件模型 (DOM)

下列程式碼範例示範如何使用 InfoPath CreateDOM 方法以及由 Microsoft.Office.Interop.InfoPath.SemiTrust 命名空間所包裝之 Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office 的成員,從 System.Xml 程式碼載入整個 XML DOM。

下列範例在表單程式碼模組的宣告區段中需要有 System.XmlusingImports 指令。此外,由於 XmlDocument 類別的 Load 方法需要 System.Security.Permissions.FileIOPermission,所以您必須使用 [表單選項] 對話方塊的 [安全性與信任] 類別,將表單範本的安全性層級設為 [完全信任]。

// Create a System.Xml XmlDocument and load an XML file.
XmlDocument doc = new XmlDocument();
doc.Load("c:\\temp\\MyFile.xml");

// Create an MSXML DOM object.
IXMLDOMDocument newDoc = thisXDocument.CreateDOM();

// Load the DOM with the XML from the System.XML object.
newDoc.loadXML(doc.DocumentElement.OuterXml);
' Create a System.Xml XmlDocument and load an XML file.
Dim doc As XmlDocument = New XmlDocument()
doc.Load("c:\temp\MyFile.xml");

' Create an MSXML DOM object.
Dim newDoc As IXMLDOMDocument = thisXDocument.CreateDOM()

' Load the DOM with the XML from the System.XML object.
newDoc.loadXML(doc.DocumentElement.OuterXml)

從 System.Xml 載入單一節點

下列程式碼範例會示範某個函數,如何使用包裝的 MSXML 5.0 createNode 方法,從 System.Xml.XmlElement 複製單一節點。

下列範例在表單程式碼模組的宣告區段中需要有 System.XmlusingImports 指令。

// This function takes a System.Xml XmlElement object and 
// an MSXML IXMLDOMDocument object, and returns an MSXML 
// IXMLDOMNode object that is a copy of the XmlElement object.
public IXMLDOMNode CloneSystemXmlElementToMsxml(
   XmlElement systemXmlElement, IXMLDOMDocument msxmlDocument)

{
   IXMLDOMNode msxmlResultNode;

   // Create a new element from the MSXML DOM using the same 
   // namespace as the XmlElement.
   msxmlResultNode = msxmlDocument.createNode(
      DOMNodeType.NODE_ELEMENT, 
      systemXmlElement.Name, 
      systemXmlElement.NamespaceURI);

   // Set the element's value.
   msxmlResultNode.text = systemXmlElement.Value.ToString();

   return msxmlResultNode;
}
' This function takes a System.Xml XmlElement object and 
' an MSXML IXMLDOMDocument object, and returns an MSXML 
' IXMLDOMNode object that is a copy of the XmlElement object.
Public Function CloneSystemXmlElementToMsxml(_
   XmlElement systemXmlElement, _
   IXMLDOMDocument msxmlDocument) As IXMLDOMNode

   Dim msxmlResultNode As IXMLDOMNode

   ' Create a new element from the MSXML DOM using the same 
   ' namespace as the XmlElement.
   msxmlResultNode = msxmlDocument.createNode(_
      DOMNodeType.NODE_ELEMENT, _
      systemXmlElement.Name, _
      systemXmlElement.NamespaceURI)

   ' Set the element's value.
   msxmlResultNode.text = systemXmlElement.Value.ToString()

   Return msxmlResultNode
End Function