我有一个linq的问题,我想从youtube xml得到多个结果,但这段代码只给我一个结果,有人知道如何解决这个问题吗?
key = @"http://gdata.youtube.com/feeds/api/videos?q="+keys+@"&orderby=relevance";
var youtube = XDocument.Load(key);
var urls = (from item in youtube.Elements("{http://www.w3.org/2005/Atom}feed")
select new
{
soundName = item.Element("{http://www.w3.org/2005/Atom}entry").Element("{http://www.w3.org/2005/Atom}title").ToString(),
url = item.Element("{http://www.w3.org/2005/Atom}entry").Element("{http://www.w3.org/2005/Atom}id").ToString(),
});
youtube xml: http://gdata.youtube.com/feeds/api/videos?q=keyword&orderby=relevance
谢谢你的帮助!
答案 0 :(得分:2)
var urls = (from item in youtube.Element("{http://www.w3.org/2005/Atom}feed").Descendants("{http://www.w3.org/2005/Atom}entry")
select new
{
soundName = item.Element("{http://www.w3.org/2005/Atom}title").ToString(),
url = item.Element("{http://www.w3.org/2005/Atom}id").ToString(),
});
答案 1 :(得分:1)
你需要获得每个entry
元素(可能有一个更简洁的方式来写这个:
var test = (from item in youtube.Elements("{http://www.w3.org/2005/Atom}feed")
from entries in item.Elements("{http://www.w3.org/2005/Atom}entry")
select new
{
soundName = entries.Element("{http://www.w3.org/2005/Atom}title").ToString(),
url = entries.Element("{http://www.w3.org/2005/Atom}id").ToString(),
});
应该给你所有的参赛作品。
编辑:Max的答案更简洁= D
答案 2 :(得分:0)
XNamespace ytNs = "http://www.w3.org/2005/Atom";
var youtube = XDocument.Load(key);
var urls = (from item in youtube.Elements(ytNs + "feed").Elements(ytNs + "entry")
select new
{
soundName = item.Element(ytNs + "title").ToString(),
url = item.Element(ytNs + "id").ToString(),
});