使用 InfoPath 2003 对象模型的表单模板项目在内部采用 Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office 来处理 XML。在托管代码中,使用由 .NET Framework 类库中的 System.Xml 命名空间所提供的 XML 支持通常更加容易。由于性质决定,MSXML 和 System.Xml 不能交换对象,因此当需要在 InfoPath 与其他托管代码之间传递 XML 数据时,需要转换 XML 数据。使用本主题中的技术,您可以将 System.Xml 对象中的 XML 数据与 InfoPath 表单代码进行交换。
要在使用 InfoPath 2003 对象模型的托管代码项目中使用 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 对象模型的成员无法分配给托管代码表单模板的表单代码中的委托。
-
如果更新表单模板的代码以使用由 Microsoft.Office.InfoPath 命名空间的成员所提供的对象模型,则会自动使用 System.Xml。但是,进行此操作时,您必须手动转换所有代码才能使用新对象模型。要将表单模板转换为使用新对象模型,请在“表单选项”对话框的“编程”类别中,单击“升级对象模型”。
从 System.Xml 中加载整个 XML 文档对象模型 (DOM)
下面的代码示例演示了如何使用 CreateDOM 方法和由 Microsoft.Office.Interop.InfoPath.SemiTrust 命名空间包装的 Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office 成员,从 System.Xml 代码中加载整个 XML DOM。
下面的示例要求在表单代码模块的声明部分对 System.Xml 使用 using 或 Imports 指令。此外,由于 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.Xml 使用 using 或 Imports 指令。
// 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