InfoPath 生成的XML文件用XPATH查询出错的问题解决(转)
时间:2010-08-18 来源:酱板猪
XPath query in Infopath forms
May 17th, 2008 Anuraj P Leave a comment Go to commentsWhile 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))
-
Denis Gravel December 3rd, 2008 at 22:57 | #1 Reply | Quote
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;
}
-
anuraj December 8th, 2008 at 02:44 | #2 Reply | Quote
Thanks Denis Gravel