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=""""><rs:data ItemCount=""0""></rs:data><rs:data ItemCount=""0""></rs:data></listitems>"

'Since the XML returned by SPS contains namespace prefixes - we need to create a XmlNamespaceManager
Dim SharePointNamespacePrefix As String = "sp"
Dim SharePointNamespaceURI As String = ""

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 (9)

  1. Mohammad Hariri says:

    I have a question about this. When I try to parse the XML feed from an RSS 2.0 source for the item:


    <title>On The Media – Jul 29, 2005</title>


    <description>why the Bush administration has given the war on terror a new name. Plus, media dispatches from Africa, Haiti and Ukraine. And, coverage of a ten-year-old heat wave.</description>

    <pubDate>Fri, 29 Jul 2005 19:55:00 GMT</pubDate>

    <enclosure url="; length="22263808" type="audio/mpeg" />



    When I try to use the following code to set a link to the value of the link:

    <asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl='<%# XPath("enclosure[@url]") %>’ >Click Here</asp:HyperLink>

    It returns a null value. Any ideas on how to work around it? My XMLDataSource is defined by:

    <asp:XmlDataSource ID="XmlDataSource1" Runat="server" Xpath="rss/channel/item" DataFile="; > </asp:XmlDataSource>



  2. Stephen Birdsall says:

    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!

  3. — Build to Perform, Architect to Evolve —

    I do not agree. Go to

  4. Elena says:

    This is great! Helped me a lot

  5. Frank Cole says:

    That kept me from pulling my hair out.  Thanks!!

  6. Satisha (WinWire) says:

    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();


               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




    are needed as the rs:data element contains the rows data and z:row contains the row data.

  7. Angelo says:

    Thanks a lot for posting this. It got me on the right track for my project.

Skip to main content