0

我试图实现的目标是从在线共享点的文档库中的文档中检索 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>5b88dd38-c2f0-4d00-8b73-80e0ca4b0eb6</id> 
  <title  /> 
  <updated>2014-03-20T14:47:21Z</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-20T14:47:21Z</updated> 
+ <author>
- <content type="application/xml">
  - <m:properties>
    + <d:CreatedBy m:type="MS.FileServices.UserInformation">
      <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: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>
</entry>
</feed>

这是我在想要列表的标题和 ID 之前使用的代码:

HttpWebRequest itemRequest =
                (HttpWebRequest)HttpWebRequest.Create(sharepointUrl.ToString() + "/_api/Web/lists(guid'" + listId + "')/Files");
            itemRequest.Method = "GET";
            itemRequest.Accept = "json;odata=verbose";
            itemRequest.Headers.Add("Authorization", "Bearer " + accessToken);
            using (HttpWebResponse itemResponse = (HttpWebResponse)itemRequest.GetResponse())
            {
                using (StreamReader itemReader = new StreamReader(itemResponse.GetResponseStream()))
                {
                    XDocument doc = XDocument.Parse(itemReader.ReadToEnd());

                    var id = doc
                        .Descendants(atom + "entry").Descendants(atom + "content").Descendants(m + "properties").Elements(d + "Url");

                    TableRow tableRow = new TableRow();
                    TableCell tableCell1 = new TableCell();
                    TableCell tableCell2 = new TableCell();
                    tableCell2.Text = id.ToString();
                    tableRow.Cells.Add(tableCell2);
                }
            }

我得到的错误基本上是说没有可以参考的元素。看起来我的流式阅读器没有正确读取 XML?任何帮助,将不胜感激。

4

1 回答 1

0

您收到的不是 xml,而是 json,正如 Accept 标头中所要求的那样。itemRequest.Accept = "json;odata=verbose";

您应该解析 json 或更改标头。我个人建议第一个选项,使用 JSON.Net 库

于 2014-08-14T12:35:18.987 回答