Specifying namespaces to use XPath queries in InfoPath

Filed under: InfoPath 2007

Learn how to define XML namespaces in InfoPath to be able to retrieve data using XPath expressions.

ADVERTISEMENTS

Using namespaces in code from within an InfoPath form

As you may already know, when you write code within an InfoPath form template, you automatically have an XmlNamespaceManager object named NamespaceManager at your disposal.

This object contains the namespaces used by the form template and allows you to perform XPath queries using namespace prefixes.

To retrieve the value of a field called field1, you could do the following:

XPathNavigator root = MainDataSource.CreateNavigator();
string fieldValue = root.SelectSingleNode(
"//my:field1", NamespaceManager).Value;

Using namespaces in code from outside an InfoPath form

When you retrieve an InfoPath form from a SharePoint library and want to access the form’s data using XPath expressions, you must manually construct an XmlNamespaceManager object that contains the namespaces you would like to use in the XPath expressions.

You can manually create an XmlNamespaceManager object on one of two ways:

  1. Manually find out what the namespaces are that you require and use the AddNamespace of a new XmlNamespaceManager to add these namespaces for use in XPath expressions. The disadvantage of using this method is that you need to know in advance what kind of InfoPath form template you are dealing with.
  2. Automatically look up all of the namespaces used in the XML of the InfoPath form and use the AddNamespace method of a new XmlNamespaceManager to add the namespaces for use in XPath expressions. The advantage of using this method is that you do not need to know in advance what kind of InfoPath form template you are dealing with, because the namespaces are automatically read in from any InfoPath form template.

You can see an example of the first method in an article I wrote for MSDN: Downloading InfoPath 2007 Forms in a Specific File Format from a SharePoint Form Library (scroll down to the Sample Code in the ASP.NET Page section).

I used the second method in How to use a Visual Studio workflow to submit data from an InfoPath form to a SharePoint list where I retrieved an InfoPath form from within a Visual Studio workflow that was deployed to SharePoint, and proceeded to retrieve values of fields that were stored in the InfoPath form.

Because I didn’t know what kind of InfoPath form the workflow would be accessing, the second method offered the flexibility of not having to restrict the workflow to only one type of InfoPath form template when I built the workflow.

I first read about the second method on Scott Hanselman’s blog in a post about how to Get namespaces from an XML Document with XPathDocument and LINQ to XML, so all credit goes to him and his readers for coming up with the code.

Ignore namespaces in XPath expressions

The purpose of defining and using namespaces in XML is to uniquely identify nodes that may have the same name within an XML document.

If an XML document contains unique names for nodes, you can choose to ignore namespaces in XPath expressions and use the local-name() XPath function to look up a field and retrieve its value as shown in the following code:

XPathNavigator root = MainDataSource.CreateNavigator();
string fieldValue = root.SelectSingleNode(
"//*[local-name()='field1']").Value;

 


Related Posts

 

Copyright: This article may not be used on web sites (whether personal or otherwise), copied, disseminated, altered, printed, published, broadcasted, or reproduced in any way without an expressed written consent. The techniques demonstrated in this article may be used within any Microsoft InfoPath project. This article is provided without any warranties. Copyright for this article is non-transferrable and remains with the author.