C# 从RESTAPI生成的XML中获取元素
我试图实现的目标是从sharepoint online上文档库中的文档检索URL。当我使用REST API向我显示此库中所有文档的列表时,我想到了下一个XML:C# 从RESTAPI生成的XML中获取元素,c#,xml,rest,sharepoint-apps,sharepoint-online,C#,Xml,Rest,Sharepoint Apps,Sharepoint Online,我试图实现的目标是从sharepoint online上文档库中的文档检索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" x
<?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>
我得到的错误基本上是说没有可参考的元素。看起来我的streamreader没有正确读取XML?
如有任何帮助,我们将不胜感激。您收到的不是xml,而是Accept标头中要求的json。 itemRequest.Accept=“json;odata=verbose” 您应该解析json或更改标头。我个人建议第一种选择,使用JSON.Net库
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);
}
}