使用 InfoPath 2003 对象模型处理 MSXML 和 System.Xml

使用 InfoPath 2003 对象模型的表单模板项目在内部利用 Microsoft XML Core Services (MSXML) 来处理 XML。 在托管代码中,使用由 .NET Framework 类库中的 System.Xml 命名空间所提供的 XML 支持通常更加容易。 由于性质决定,MSXML 和 System.Xml 不能交换对象,因此当需要在 InfoPath 与其他托管代码之间传递 XML 数据时,需要转换 XML 数据。 使用本主题中的技术,您可以将 System.Xml 对象中的 XML 数据与 InfoPath 表单代码进行交换。

若要在使用 InfoPath 2003 对象模型的托管代码项目中使用 System.Xml 命名空间的成员,必须在 Visual Studio 2012 内的“添加引用”对话框中的“.NET”选项卡上添加对 System.Xml 的引用。

备注

  • 若需查看有关 MSXML 的参考信息,请参阅 MSXML SDK。

  • Microsoft.Office.Interop.InfoPath.SemiTrust 命名空间封装的 MSXML 对象模型的成员无法分配给托管代码表单模板的表单代码中的委托。

  • 如果更新表单模板的代码,以使用 Microsoft.Office.InfoPath 命名空间的成员所提供的对象模型,则会以本机方式使用 System.Xml。 但执行此操作时,必须手动转换所有代码以使用新的对象模型。 若要转换表单模板以使用新的对象模型,在“表单选项”对话框的“编程”类别中,单击“升级 OM”。

从 System.Xml 中加载整个 XML 文档对象模型 (DOM)

下面的代码示例演示如何使用 InfoPath CreateDOM 方法和由 Microsoft.Office.Interop.InfoPath.SemiTrust 命名空间成员封装的 Microsoft XML Core Services 成员,从 System.Xml 代码中加载整个 XML DOM。

下面的示例要求在表单代码模块的声明部分中对 System.Xml 使用 usingImports 指令。 此外,由于 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 中加载单个节点

以下代码示例演示了一个函数,该函数演示如何从 System.Xml克隆单个节点。 使用包装的 MSXML createNode 方法的 XmlElement

下面的示例要求在表单代码模块的声明部分对 System.Xml 使用 usingImports 指令。

// 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