从REST API调用中获取xml节点值
本文关键字:xml 节点 获取 REST API 调用 | 更新日期: 2023-09-27 17:50:58
我们正在尝试使用c#从下面列表的条目中获取Url。
<?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);
编辑:使用Alex Thompson提供的解决方案,我们已经能够缩小问题的范围。编辑完代码后,我一直在调试我的程序,并注意到下面的XML是我返回到流阅读器中的全部内容:
<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。如果有人能给我指出这个问题的原因,我将不胜感激。我
你可以这样做:
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
});