dotnet thoughts 

a dotnet developer's technical blog

XPath query in Infopath forms

While working in Infopath, some times it is required to Query Infopath forms using XPath and XmlDocument. But the Infopath XML contains a “my” prefix. So simple XDocument.Select will throw the an XPath exception – Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function. To fix this issue you need a namespace manager object which required to query Infopath forms.

Dim doc As New System.Xml.XmlDocument
doc.Load("D:\Infopath.xml")
Dim node As Xml.XmlNode = doc.SelectSingleNode("//my:Root") 'It will throw error because it doesn't have the Namespace.

Function will return the Namespace Manager based on the document given.

Function GetNameSpaceManager(ByVal Document As XmlDocument) As XmlNameSpaceManager
Dim objXmlNamespaceManager As New XmlNamespaceManager(Document.NameTable)
objXmlNamespaceManager.AddNamespace("my", Document.DocumentElement.GetNamespaceOfPrefix("my"))
Return objXmlNamespaceManager
End Function

And you can query infopath like this.

Dim doc As New System.Xml.XmlDocument
doc.Load("D:\Infopath.xml")
Dim node As Xml.XmlNode = doc.SelectSingleNode("//my:Root", GetNameSpaceManager(doc))

No Responses to “XPath query in Infopath forms”

  1. It solved my problem! Thank You !

    Here for those of you who would like to use the c# version of the GetNameSpaceManager function :


    protected XmlNamespaceManager GetNameSpaceManager(XmlDocument Document)
    {
    XmlNamespaceManager objXmlNamespaceManager = new XmlNamespaceManager(Document.NameTable);
    objXmlNamespaceManager.AddNamespace("dc", Document.DocumentElement.GetNamespaceOfPrefix("dc"));
    return objXmlNamespaceManager;
    }

    Comment by Denis Gravel — December 3, 2008 @ 10:57 pm

  2. Thanks Denis Gravel :)

    Comment by anuraj — December 8, 2008 @ 2:44 am

RSS feed for comments on this post. TrackBack URL

Leave a Response

*