从REST API生成的XML中获取元素



我想要实现的目标是从sharepoint在线文档库中的文档检索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 ?

您正在接收的不是xml,而是json,正如Accept报头所请求的那样。itemRequest。Accept = "json;odata=verbose";

你应该要么解析json,要么更改标题。我个人建议第一种选择,使用JSON。网络图书馆

相关内容

  • 没有找到相关文章

最新更新