Share via


Using XPath with XML returned by SharePoint Web services

I've seen quite often people writing code that parses thru XML returned by SPS Web services as strings instead of using XPath - cause the XmlDocument throws this error all the time if you dont use an XmlNameSpaceManager

"Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function."

Heres a snippet that tells you how to use the XmlNameSpaceManager to use XPath with the XmlDocument.

Dim Document As New XmlDocument
Dim xml As String

xml = "<listitems xmlns:s=""uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882"" xmlns:dt=""uuid:C2F41010-65B3-11d1-A29F-00AA00C14882"" xmlns:rs=""urn:schemas-microsoft-com:rowset"" xmlns:z=""#RowsetSchema"" TimeStamp=""2004-12-20T20:57:26Z"" xmlns=""https://schemas.microsoft.com/sharepoint/soap/""><rs:data ItemCount=""0""></rs:data><rs:data ItemCount=""0""></rs:data></listitems>"
Document.LoadXml(xml)

'Since the XML returned by SPS contains namespace prefixes - we need to create a XmlNamespaceManager
Dim SharePointNamespacePrefix As String = "sp"
Dim SharePointNamespaceURI As String = "https://schemas.microsoft.com/sharepoint/soap/"

Dim ListItemsNamespacePrefix As String = "z"
Dim ListItemsNamespaceURI As String = "#RowsetSchema"

Dim PictureLibrariesNamespacePrefix As String = "s"
Dim PictureLibrariesNamespaceURI As String = "uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882"

Dim WebPartsNamespacePrefix As String = "dt"
Dim WebPartsNamespaceURI As String = "uuid:C2F41010-65B3-11d1-A29F-00AA00C14882"

Dim DirectoryNamespacePrefix As String = "rs"
Dim DirectoryNamespaceURI As String = "urn:schemas-microsoft-com:rowset"

'now associate with the xmlns namespaces (part of all XML nodes returned
'from SharePoint) a namespace prefix which we can then use in the queries
Dim NamespaceMngr As XmlNamespaceManager
NamespaceMngr = New XmlNamespaceManager(Document.NameTable)

NamespaceMngr.AddNamespace(SharePointNamespacePrefix, SharePointNamespaceURI)
NamespaceMngr.AddNamespace(ListItemsNamespacePrefix, ListItemsNamespaceURI)
NamespaceMngr.AddNamespace(PictureLibrariesNamespacePrefix, PictureLibrariesNamespaceURI)
NamespaceMngr.AddNamespace(WebPartsNamespacePrefix, WebPartsNamespaceURI)
NamespaceMngr.AddNamespace(DirectoryNamespacePrefix, DirectoryNamespaceURI)

'run the XPath query and return the result nodes
Dim xNodeList As XmlNodeList
xNodeList = Document.SelectNodes("//z:row[@ows_Title]", NamespaceMngr)

Mohammed Jeelani

Comments

  • Anonymous
    August 01, 2005
    The comment has been removed

  • Anonymous
    October 20, 2006
    Seeing how you set it out helped me put the final pieces together in this lovely coughs puzzle.  I hadn't realised that your AddNamespace calls need to have exactly the same text in them as is shown in the xmlns declaration, ie xmlns:z="#RowsetSchema" has to be represented by NamespaceMngr.AddNamespace("z", "#RowsetSchema"). Thanks for keeping my project on track!

  • Anonymous
    November 26, 2006
    -- Build to Perform, Architect to Evolve -- I do not agree. Go to http://www.hotelshouse.info/woe_Germany/organ_Nordrhein-Westfalen/pyjamas_D%C3%BCsseldorf_1.html

  • Anonymous
    December 05, 2006
    -- Build to Perform, Architect to Evolve -- I do not agree. Go to http://www.apartments.waw.pl

  • Anonymous
    October 04, 2007
    This is great! Helped me a lot

  • Anonymous
    October 12, 2007
    That kept me from pulling my hair out.  Thanks!!

  • Anonymous
    December 01, 2007
    Yes. this is a good post. Easier way is to get the Innerxml of the Webservice Return param and just add the following code XmlDocument xDoc = new XmlDocument();            xDoc.LoadXml(SPXml);            string ListItemsNamespacePrefix = "z";            string ListItemsNamespaceURI = "#RowsetSchema";            string DirectoryNamespacePrefix = "rs";            string DirectoryNamespaceURI = "urn:schemas-microsoft-com:rowset";            XmlNamespaceManager NamespaceMngr = new XmlNamespaceManager(xDoc.NameTable);            NamespaceMngr.AddNamespace(ListItemsNamespacePrefix, ListItemsNamespaceURI);            NamespaceMngr.AddNamespace(DirectoryNamespacePrefix, DirectoryNamespaceURI);            XmlNodeList xNList = xDoc.SelectNodes("//rs:data//z:row", NamespaceMngr); I dont think that the SharePointNamespaceURI PictureLibrariesNamespaceURI WebPartsNamespaceURI are needed as the rs:data element contains the rows data and z:row contains the row data.

  • Anonymous
    July 07, 2008
    Thanks a lot for posting this. It got me on the right track for my project.

  • Anonymous
    June 18, 2009
    PingBack from http://patiocushionsource.info/story.php?id=2300