Tuesday, December 22, 2009

SharePoint: Parse Response XML from List.asmx

If you use SharePoint web service then the response of invocation of most of the methods will be in XML format. Sometimes you need to parse the XML to find the operation status. For example when you use List web service’s UpdateListItems for insert, update or delete operation the result of the operation will be returned in  xml.

To parse returned xml you need to know the namespaces of the XML. The following code snippet shows different namespaces used in the response XML.

XmlDocument document=new XmlDocument(); 
document.LoadXml(responseXml); 
var namespaceManager = new XmlNamespaceManager(document.NameTable); 

//sharepoint namespace 
namespaceManager.AddNamespace("sp", "http://schemas.microsoft.com/sharepoint/soap/"); 
//list item namespace 
namespaceManager.AddNamespace("z", "#RowsetSchema"); 
//picture library namespace 
namespaceManager.AddNamespace("s", "uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882"); 
//web part namespace 
namespaceManager.AddNamespace("dt", "uuid:C2F41010-65B3-11d1-A29F-00AA00C14882"); 
//directory namespace 
namespaceManager.AddNamespace("rs", "urn:schemas-microsoft-com:rowset"); 


In the above code snippet you can see there are five namespaces defined. Once you have defined these namespaces you can select nodes of particular namespace as shown below. The following code snippet select all row nodes under z namespace (of the format <z:row>).

document.SelectNodes("//z:row", namespaceManager);

1 comment:

Note: Only a member of this blog may post a comment.