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:

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.

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.

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.

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:

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.