Working with MSXML and System.Xml Using the InfoPath 2003 Object Model
Form template projects that work with the InfoPath 2003 object model use Microsoft XML Core Services (MSXML) internally to work with XML. In managed code, it is often easier to use the XML support provided by the System.Xml namespace in the .NET Framework class library. MSXML and System.Xml cannot exchange objects natively, so whenever you need to pass XML data between InfoPath and other managed code, XML data needs to be converted. You can exchange XML data from System.Xml objects with InfoPath form code by using the techniques in this topic.
To use members of the System.Xml namespace in a managed code project that uses the InfoPath 2003 object model, you must add a reference to System.Xml on the .NET tab of the Add Reference dialog box in Visual Studio 2012.
Notes
To view reference information about MSXML, see the MSXML SDK.
Members of the MSXML object model that are wrapped by the Microsoft.Office.Interop.InfoPath.SemiTrust namespace cannot be assigned to delegates in the form code of managed-code form templates.
If you update the code of your form template to use the object model provided by members of the Microsoft.Office.InfoPath namespace, System.Xml is used natively. However, when doing so, you must manually convert all of your code to use the new object model. To convert your form template to use the new object model, in the Programming category of the Form Options dialog box, click Upgrade OM.
Loading an Entire XML Document Object Model (DOM) from System.Xml
The following code sample demonstrates how to load an entire XML DOM from System.Xml code using the InfoPath CreateDOM method and the members of Microsoft XML Core Services that are wrapped by members of the Microsoft.Office.Interop.InfoPath.SemiTrust namespace.
The following examples require a using or Imports directive for System.Xml in the declarations section of the form code module. Additionally, because the Load method of the XmlDocument class requires System.Security.Permissions.FileIOPermission, you must configure the security level of the form template as Full Trust using the Security and Trust category of the Form Options dialog box.
// 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)
Loading a Single Node from System.Xml
The following code sample shows a function that demonstrates how to clone a single node from a System.Xml. XmlElement using the wrapped MSXML createNode method.
The following examples require a using or Imports directive for System.Xml in the declarations section of the form code module.
// 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