2014-03-19 37 views
0

我們試圖通過使用c#從下面列表的條目中獲取Url。從REST API調用中獲取xml節點值

<?xml version="1.0" encoding="utf-8" ?> 
<feed xml:base="https://thomasmorestudent17.sharepoint.com/sites/devtest/_api/" xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml"> 
    <id>b82076e4-3e36-4b09-bbed-3d14e0bf948f</id> 
    <title /> 
    <updated>2014-03-19T10:21:14Z</updated> 
- <entry> 
    <id>https://thomasmorestudent17.sharepoint.com/sites/devtest/_api/Web/Lists(guid'ab8811c5-0d39-457c-8fd1-c15a45c78f89')/files('Aanleiding en achtergrond van het project.docx')</id> 
    <category term="MS.FileServices.File" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" /> 
    <link rel="edit" href="Web/Lists(guid'ab8811c5-0d39-457c-8fd1-c15a45c78f89')/files('Aanleiding%20en%20achtergrond%20van%20het%20project.docx')" /> 
    <title /> 
    <updated>2014-03-19T10:21:14Z</updated> 
    - <author> 
     <name /> 
    </author> 
    - <content type="application/xml"> 
    - <m:properties> 
     - <d:CreatedBy m:type="MS.FileServices.UserInformation"> 
      <d:Id>9</d:Id> 
      <d:Name>Thomas More</d:Name> 
     </d:CreatedBy> 
     <d:ETag>"{ECAEE072-FEDD-4FF6-8A27-1EFF131B0064},1"</d:ETag> 
     <d:Id>Aanleiding en achtergrond van het project.docx</d:Id> 
     - <d:LastModifiedBy m:type="MS.FileServices.UserInformation"> 
      <d:Id>9</d:Id> 
      <d:Name>Thomas More</d:Name> 
     </d:LastModifiedBy> 
     <d:Name>Aanleiding en achtergrond van het project.docx</d:Name> 
     <d:Size m:type="Edm.Int32">21616</d:Size> 
     <d:TimeCreated m:type="Edm.DateTime">2014-03-14T17:24:25Z</d:TimeCreated> 
     <d:TimeLastModified m:type="Edm.DateTime">2014-03-14T17:24:25Z</d:TimeLastModified> 
     <d:Url>/sites/devtest/Shared Documents/Aanleiding en achtergrond van het project.docx</d:Url> 
    </m:properties> 
    </content> 

不過,我們遇到了我們的代碼有問題。 調試時,webRequest的url是正確的,但itemList保持空白。

HttpWebRequest itemRequest = (HttpWebRequest)HttpWebRequest.Create(sharepointUrl.ToString() + "/_api/Web/lists(guid'" + listId + "')/files"); 
     itemRequest.Method = "GET"; 
     itemRequest.Accept = "application/atom+xml"; 
     itemRequest.ContentType = "application/atom+xml;type=entry"; 
     itemRequest.Headers.Add("Authorization", "Bearer " + accessToken); 
     HttpWebResponse itemResponse = (HttpWebResponse)itemRequest.GetResponse(); 
     StreamReader itemReader = new StreamReader(itemResponse.GetResponseStream()); 

     var itemXml = new XmlDocument(); 
     itemXml.LoadXml(itemReader.ReadToEnd()); 

     var itemList = itemXml.SelectNodes("//atom:entry/atom:content/m:properties/d:Url", xmlnspm); 

編輯:

使用由亞歷克斯·湯普森提供的解決方案,我們已經能夠縮小問題列表。編輯代碼後,我一直在調試我的程序,發現下面的XML是我得到的返回到我的StreamReader:

<feed xml:base="https://thomasmorestudent17.sharepoint.com/sites/devtest/_api/" xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml"> 
    <id>273fa8b2-b789-41d0-9edf-01eb12657299</id> 
    <title /> 
    <updated>2014-03-20T09:58:18Z</updated> 
    <author> 
    <name /> 
    </author> 
</feed> 

這無疑是不應該返回XML。如果有人能在正確的方向是什麼這個問題的原因可能是指向我,那將是極大的appreciated.I

回答

0

你可以做這樣的事情:

private static readonly XNamespace d = "http://schemas.microsoft.com/ado/2007/08/dataservices"; 
private static readonly XNamespace m = "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"; 

... 

XDocument doc = XDocument.Load(itemReader.ReadToEnd()); 
var itemsList = doc.Descendants(m + "properties").Descendants(d + "Url").Select(item => item.Value); 

如果你想選擇多個值轉換爲對象,您可以執行類似操作:

private static readonly XNamespace d = "http://schemas.microsoft.com/ado/2007/08/dataservices"; 
private static readonly XNamespace m = "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"; 

... 

var itemsList doc.Descendants(m + "properties").Select(
    item => new Item() 
    { 
     Id = item.Element(d + "Id").Value, 
     Name = item.Element(d + "Name").Value, 
     Url = item.Element(d + "Url").Value 
    });