Web services, XmlNode, and XPATH

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:

xmlns="http://schemas.microsoft.com/sharepoint/soap/directory/">
>

MemberID="3" Mask="-1" MemberIsUser="False" MemberGlobal="True" GroupName="IT Projects Owners" />
MemberID="4" Mask="138612833" MemberIsUser="False" MemberGlobal="True" GroupName="IT Projects Visitors" />
MemberID="5" Mask="1011028719" MemberIsUser="False" MemberGlobal="True" GroupName="IT Projects Members" />
MemberID="1073741823" Mask="-1" MemberIsUser="True" MemberGlobal="False" UserLogin="SHAREPOINT\system" />
>

>

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

Here is another example on Stackoverflow.