2014-03-19 5 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 
    }); 
관련 문제