C# 从RESTAPI调用获取xml节点值
我们正在尝试使用c#从下面列表的条目中获取Url 编辑: 使用Alex Thompson提供的解决方案,我们能够缩小问题列表。编辑代码后,我一直在调试我的程序,并注意到以下XML是我返回到streamreader中的全部内容:C# 从RESTAPI调用获取xml节点值,c#,xml,rest,sharepoint-online,C#,Xml,Rest,Sharepoint Online,我们正在尝试使用c#从下面列表的条目中获取Url 编辑: 使用Alex Thompson提供的解决方案,我们能够缩小问题列表。编辑代码后,我一直在调试我的程序,并注意到以下XML是我返回到streamreader中的全部内容: <feed xml:base="https://thomasmorestudent17.sharepoint.com/sites/devtest/_api/" xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://s
<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>
273fa8b2-b789-41d0-9edf-01eb12657299
2014-03-20T09:58:18Z
这无疑不是它应该返回的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
});
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
});