为什么我的xpath不起作用

时间:2014-08-24 10:04:21

标签: sharepoint xpath

我正在使用SharePoint Web服务从网站检索内容类型。输出如下:

 <ContentTypes ContentTypeOrder="0x010300971A94A609AC5F4390A1FF87A26CD05D" xmlns="http://schemas.microsoft.com/sharepoint/soap/">
 <ContentType Name="Issue" ID="0x010300971A94A609AC5F4390A1FF87A26CD05D" 
    Description="Track an issue or problem." 
    Scope="https://practiv1.sharepoint.com/devtest/Lists/issueTracking" 
    Version="0" 
    BestMatch="TRUE">
     <XmlDocuments>
         <XmlDocument NamespaceURI="http://schemas.microsoft.com/sharepoint/v3/contenttype/forms">
         <FormTemplates xmlns="http://schemas.microsoft.com/sharepoint/v3/contenttype/forms">
          <Display>ListForm</Display> 
          <Edit>ListForm</Edit> 
          <New>ListForm</New> 
          </FormTemplates>
          </XmlDocument>
      </XmlDocuments>
  </ContentType>
 <ContentType Name="Folder" 
    ID="0x01200049A00CD1A9F3944A9AE7BCCAC15B02D4" 
    Description="Create a new folder." 
    Scope="https://practiv1.sharepoint.com/devtest/Lists/issueTracking" 
    Version="0">
     <XmlDocuments>
         <XmlDocument NamespaceURI="http://schemas.microsoft.com/sharepoint/v3/contenttype/forms">
             <FormTemplates xmlns="http://schemas.microsoft.com/sharepoint/v3/contenttype/forms">
              <Display>ListForm</Display> 
              <Edit>ListForm</Edit> 
              <New>ListForm</New> 
              </FormTemplates>
          </XmlDocument>
      </XmlDocuments>
  </ContentType>

我想使用xpath来检索项目。但是我使用的路径就像&#34; // ContentType&#34;或者&#34; / ContentTypes / ContentType&#34;,我找不到任何东西:

var listService = new ListWebService.Lists(); 
listService.Url = "xxx.sharepoint.com/xxx/_vti_bin/Lists.asmx";
var contents = listService.GetListContentTypes("issueTracking", "0x01"); 

有人可以帮我解决我的xpath错误吗?

1 个答案:

答案 0 :(得分:1)

在处理具有默认命名空间(xmlns="...")的XML时,这是一个常见问题。声明前缀的节点及其所有后代(如果未另外明确指定)将在默认命名空间中考虑。

您需要注册一个指向命名空间URI的前缀,并在XPath中使用该前缀,例如:

var nsManager = new XmlNamespaceManager(new NameTable());
nsManager.AddNamespace("d", "http://schemas.microsoft.com/sharepoint/soap/");
var result = contents.SelectNodes("//d:ContentType", nsManager);
//or using the other XPath : "/d:ContentTypes/d:ContentType"