处理SharePoint UpdateListItems XML响应

时间:2012-06-19 17:11:36

标签: c# xml xpath sharepoint-2007

我正在使用列表Web服务将一些项目插入SharePoint 2007列表。我正在尝试编写一些代码来处理响应中报告的任何错误,但是导航XML并没有按预期工作。我正在尝试获取Result元素的集合,然后检查每个Result的ErrorCode子元素以获取错误代码。当我尝试获取第二个结果的ErrorCode时,它似乎再次给我第一个结果的ErrorCode,即使我在第二个Result元素上调用SelectSingleNode。我究竟做错了什么?这是我的数据和代码(大多数z:省略了行属性以保持简短):

SharePoint响应:

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

代码:

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

输出:

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

1 个答案:

答案 0 :(得分:1)

尝试:

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

我认为问题在于//sp:ErrorCode表示“所有错误代码节点都是从文档的根目录中”,而您想要的是结果节点下的错误代码节点。