Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
200 views
in Technique[技术] by (71.8m points)

sharepoint - Why my xpath doesn't work

I am using SharePoint webservice to retrieve content type from the site. The output looks like this:

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

I want to use xpath to retrieve items. But I used the path like "//ContentType" or "/ContentTypes/ContentType", I couldn't find anything :

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

Can someone help me what wrong with my xpath?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This is a common problem when dealing with XML having default namespace (xmlns="..."). The node where the prefix declared and all it's descendants, if not explicitly specified otherwise, are considered in the default namespace.

You need to register a prefix that points to the namespace URI and use that prefix in your XPath, for example :

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"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...