InfoPath IXMLDOMDocument Reference to undeclared namespace prefixBy making use of the Microsoft Office Interop libraries it is possible to fill an InfoPath form automatically. This can be useful when you would like to run a large batch of initial settings.

Trying to set a query property in the DOM of a data source I received the error “Reference to undeclared namespace prefix: ‘tns'”. The reason of this error is that the xml contains multiple namespaces and that you have to explicitly define the namespace to use.

By default the DOM property you can access from the XDocument are of the type IXMLDOMDocument. But working with this type, does not allow you to set the required namespace for your selections. To get around the issue convert the IXMLDOMDocument to a IXMLDOMDocument2 which makes it possible to define a “SelectionNamespaces” property. If you fill this property with the required documentElement’s namespace, the xpath selection of your node will succeed and the error is gone.

Below is an example of the Data source’s IXMLDOMDocument2 content (xml) and some demo code to select the required node.


IXMLDOMDocument2 content

<dfs:myFields xmlns:dfs=”http://schemas.microsoft.com/office/infopath/2003/
dataFormSolution” xmlns:tns=”http://tempuri.org/” xmlns:my=”http://schemas.microsoft.com/office/infopath/2003/myXSD/
2007-09-05T13:53:48″ xmlns:_xdns0=”http://schemas.microsoft.com/office/infopath/2003/
changeTracking” xmlns:soap=”http://schemas.xmlsoap.org/soap/envelope/” xmlns:xsi=”http://www.w3.org/2001/
XMLSchema-instance” xmlns:xsd=”http://www.w3.org/2001/XMLSchema” xmlns:diffgr=”urn:schemas-microsoft-com:
xml-diffgram-v1″ xmlns:msdata=”urn:schemas-microsoft-com:xml-msdata”>
 <dfs:queryFields>
  <tns:SearchEmployee>
   <tns:name></tns:name>
   <tns:siteUrl>http://portal.site.org/</tns:siteUrl>
   <tns:listName>Employees</tns:listName>
  </tns:SearchEmployee>
 </dfs:queryFields>
 <dfs:dataFields>
  <tns:SearchEmployeeResponse>
   <tns:SearchEmployeeResult>
    <NewDataSet>
     <Employees>
      <ID></ID>
      <Email></Email>
      <Accountname></Accountname>
      <Created></Created>
     </Employees>
    </NewDataSet>
   </tns:SearchEmployeeResult>
  </tns:SearchEmployeeResponse>
 </dfs:dataFields>
</dfs:myFields>

Example code

private static void DemoCode()
{
 //Start application and load the xsn file
 Application application = new ApplicationClass();
 XDocument document = application.XDocuments.NewFromSolution(“c:test.xsn”);

 //Get the XML Document Object Model of the webservice datasource
 //and load/convert the type IXMLDOMDocument as IXMLDOMDocument2
 IXMLDOMDocument2 docDom = document.DataObjects[“WebService_SearchEmployee”].DOM as IXMLDOMDocument2;

 //retrieve the namespace and set the SelectionNamespaces property
 string tsnNamespace = GetTNSNamespace(docDom);
 docDom.setProperty(“SelectionNamespaces”, tsnNamespace);

 //Get the required node
 IXMLDOMNode nodeEmployeeName = docDom.selectSingleNode(“//tns:name”);
}

private static string GetTNSNamespace(IXMLdocDomument2 xmlDoc)
{
 return xmlDoc.documentElement.attributes.getNamedItem(“xmlns:tns”).xml;
}