Welcome to MSDN Blogs Sign in | Join | Help

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=""http://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 = "
http://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

Published Saturday, February 26, 2005 10:23 PM by mjeelani
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: Using XPath with XML returned by SharePoint Web services

Monday, August 01, 2005 2:15 PM by Mohammad Hariri
I have a question about this. When I try to parse the XML feed from an RSS 2.0 source for the item:
<item>
<title>On The Media - Jul 29, 2005</title>
<link>http://www.onthemedia.org/</link>
<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="http://wnyc.vo.llnwd.net/o1/otm/otm072905pod.mp3" length="22263808" type="audio/mpeg" />
<itunes:duration>00:53:00</itunes:duration>
</item>

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="http://www.onthemedia.org/index.xml" > </asp:XmlDataSource>

Thanks,
Mo

# re: Using XPath with XML returned by SharePoint Web services

Friday, October 20, 2006 10:49 AM by Stephen Birdsall

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!

# pyjamas D%C3%BCsseldorf

Monday, November 27, 2006 6:19 AM by pyjamas D%C3%BCsseldorf

-- 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

# I think it

Wednesday, December 06, 2006 2:10 AM by warsaw hotels

-- Build to Perform, Architect to Evolve --

I do not agree. Go to http://www.apartments.waw.pl

# re: Using XPath with XML returned by SharePoint Web services

Thursday, October 04, 2007 3:15 PM by Elena

This is great! Helped me a lot

# re: Using XPath with XML returned by SharePoint Web services

Friday, October 12, 2007 1:27 PM by Frank Cole

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

# re: Using XPath with XML returned by SharePoint Web services

Saturday, December 01, 2007 2:58 PM by Satisha (WinWire)

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.

# re: Using XPath with XML returned by SharePoint Web services

Monday, July 07, 2008 12:54 PM by Angelo

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

# Mohammed Jeelani s Blog Using XPath with XML returned by SharePoint | patio cushions

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker