Web services, XmlNode, and XPATH
By Kit
I was writing a little web services app to help delete individual permissions from a site and thought about trying XPATH on the result to get what I needed. I had some trouble so thought I’d put together a small example for later reference.
The XML from the Permissions GetPermissionCollection web service looks something like:
>
>
The main heartache I had when trying to use XPATH was that the XML uses a default namespace (the xmlns=”” part). In order to select the Permission nodes, you need to use a XmlNamespaceManager:
Permissions webSvc = new Permissions(site.Url.ToString());
XmlNode result = webSvc.GetPermissionCollection(site.Name, "Web");
XmlNamespaceManager nsmgr = new XmlNamespaceManager(result.OwnerDocument.NameTable);
nsmgr.AddNamespace("sp", "http://schemas.microsoft.com/sharepoint/soap/directory/");
string xpathQuery = "sp:Permissions/sp:Permission";
XmlNodeList nodeList = result.SelectNodes(xpathQuery, nsmgr);
XmlNode result = webSvc.GetPermissionCollection(site.Name, "Web");
XmlNamespaceManager nsmgr = new XmlNamespaceManager(result.OwnerDocument.NameTable);
nsmgr.AddNamespace("sp", "http://schemas.microsoft.com/sharepoint/soap/directory/");
string xpathQuery = "sp:Permissions/sp:Permission";
XmlNodeList nodeList = result.SelectNodes(xpathQuery, nsmgr);
Here is another example on Stackoverflow.