我在使用linq在c#wpf应用程序中从XML获取信息时遇到了麻烦。 以下是我使用的代码。从xml使用LINQ获取信息
public class YouTubeInfo
{
public string LinkUrl { get; set; }
public string EmbedUrl { get; set; }
public string ThumbNailUrl { get; set; }
public string Title { get; set; }
public int Duration { get; set; }
}
public class YouTubeProvider
{
const string SEARCH = "http://gdata.youtube.com/feeds/api/videos?q={0}&alt=rss&start-index={1}&max-results={2}&v=1";
const string Most_popular = "http://gdata.youtube.com/feeds/api/standardfeeds/KR/most_popular?time=today&alt=rss&start-index={1}&max-results={2}&v=2";
//const string Entertainment = "https://gdata.youtube.com/feeds/api/standardfeeds/KR/most_popular_Entertainment?start-index=1&max-results=2";
#region Load Videos From Feed
public static int search_based;
static string search;
public static List<YouTubeInfo> LoadVideosKey(string keyWord, int start, int limit)
{
try
{
switch (search_based)
{
case 0: search = SEARCH; break;
case 1: search = Most_popular; break;
}
var xraw = XElement.Load(string.Format(search, keyWord, start, limit));
var xroot = XElement.Parse(xraw.ToString());
var links = (from item in xroot.Element("channel").Descendants("item")
select new YouTubeInfo
{
LinkUrl = item.Element("link").Value,
Title = item.Element("title").Value,
EmbedUrl = GetEmbedUrlFromLink(item.Element("link").Value),
ThumbNailUrl = GetThumbNailUrlFromLink(item),
Duration = GetDuration(item),
}).Take(limit);
return links.ToList<YouTubeInfo>();
}
catch (Exception e)
{
Trace.WriteLine(e.Message, "ERROR");
}
return null;
}
我想从这个XML
有一个在你的问题很多不必要的数据。发布XML(文档,不链接到它),查询和预期结果/您当前的代码有什么问题。 – MarcinJuraszek