Handling SharePoint UpdateListItems XML Response

1

I'm inserting some items into a SharePoint 2007 list using the Lists web service. I'm trying to write some code to handle any errors reported in the response, but navigating the XML is not working as expected. I'm attempting to get a collection of Result elements, then check each Result's ErrorCode child element for an error code. When I try to get the ErrorCode of the second result, it appears to give me the first result's ErrorCode again even though I'm calling SelectSingleNode on the second Result element. What am I doing wrong? Here is my data and code (with most of the z:row attributes omitted to keep it short):

SharePoint response:

- <Results xmlns="http://schemas.microsoft.com/sharepoint/soap/">
    - <Result ID="1,New">
          <ErrorCode>0x00000000</ErrorCode> 
          <ID /> 
          <z:row ows_ContentTypeId="0x0100760B0FF12756D249834F5B18A46B1A31" ows_Title="Seas-a-1-1-Target1" ows_PointID="1" ows_X_x0020_Value="355.000000000000" ... xmlns:z="#RowsetSchema" /> 
      </Result>
    - <Result ID="2,New">
          <ErrorCode>0x80020005</ErrorCode> 
          <ErrorText>The operation failed because an unexpected error occurred. (Result Code: 0x80020005)</ErrorText> 
      </Result>
  </Results>

Code:

System.Xml.XmlNode response = listService.UpdateListItems("SCs", batchElement);

XmlNamespaceManager nsm = new XmlNamespaceManager(response.OwnerDocument.NameTable);
nsm.AddNamespace("sp", response.NamespaceURI);

XmlNodeList results = response.SelectNodes("//sp:Result", nsm);
foreach (XmlNode result in results)
{
    System.Diagnostics.Debug.WriteLine(result.OuterXml);
    XmlNode node = result.SelectSingleNode("//sp:ErrorCode", nsm);
    System.Diagnostics.Debug.WriteLine(node.OuterXml);
}

Output:

<Result ID="1,New" xmlns="http://schemas.microsoft.com/sharepoint/soap/"><ErrorCode>0x00000000</ErrorCode><ID /><z:row ows_ContentTypeId="0x0100760B0FFF2756D249834F5B18A46B1A31" ows_Title="Seas-a-1-1-Target1" ows_PointID="1" ows_X_x0020_Value="355.000000000000" ... xmlns:z="#RowsetSchema" /></Result>
<ErrorCode xmlns="http://schemas.microsoft.com/sharepoint/soap/">0x00000000</ErrorCode>
<Result ID="2,New" xmlns="http://schemas.microsoft.com/sharepoint/soap/"><ErrorCode>0x80020005</ErrorCode><ErrorText>The operation failed because an unexpected error occurred. (Result Code: 0x80020005)</ErrorText></Result>
<ErrorCode xmlns="http://schemas.microsoft.com/sharepoint/soap/">0x00000000</ErrorCode>
c#
xml
xpath
sharepoint-2007
asked on Stack Overflow Jun 19, 2012 by xr280xr

1 Answer

1

Try with:

XmlNode node = result.SelectSingleNode(".//sp:ErrorCode", nsm); 

I think that the problem is that //sp:ErrorCode means 'all error code nodes starying from the root of the document', whereas what you want is the the error code nodes under the result node.

answered on Stack Overflow Jun 19, 2012 by MiMo

User contributions licensed under CC BY-SA 3.0