解析YouTube的使用LINQ供稿 [英] Parse youtube feeds with linq
问题描述
我想分析的YouTube频道的Atom提要。
这里是RSS原子饲料链接
的 http://gdata.youtube.com/feeds/api/users/cokestudio/uploads?orderby=updated
块引用>
列表< YTFeeds> lstYT =新的List< YTFeeds>();
的XDocument xDocumentYT = XDocument.Load(使用Server.Mappath(XMLFile.xml));
的XNamespace的xmlns =http://www.w3.org/2005/Atom;
lstYT.AddRange((从xDocumentYT.Descendants条目(的xmlns +项),元素符号(xmlns +媒体:集团)
选择新YTFeeds
{
标题= entry.Element号(xmlns +媒体:标题)。价值
说明= entry.Element号(xmlns +媒体:说明)。价值
=视频entry.Elements号(xmlns +媒体:球员。)的ElementAt(1)属性约(URL传媒)值,
图像= entry.Elements号(xmlns +:)的ElementAt(1)属性约(缩略图URL) .value的
})了ToList());
我得到它说的错误
无效字符或十六进制编码:
。我想从标签元素:<介质:组>
请建议
解决方案在使用一个命名空间写出元素的名称,你要离开了前缀,它会为您处理。在这种情况下,你需要一个单独的命名空间的实例才能够获得
传媒
元素。所以访问标题,描述等,应该是这样的:VAR DOC = XDocument.Load(使用Server.Mappath(@ XMLFile.xml));
的XNamespace的xmlns =http://www.w3.org/2005/Atom;
的XNamespace媒体=http://search.yahoo.com/mrss/;
VAR的查询=从doc.Root.Elements条目
(+的xmlns项)
让GRP = entry.Element(媒体+集团)
选择新YTFeeds
{
标题=(字符串)grp.Element(媒体+题),
说明=(字符串)grp.Element(媒体+说明),
视频=(字符串)grp.Element(媒体+玩家)属性(URL),
图像= grp.Elements(媒体+缩略图)
。选择(E =>(字符串)e.Attribute(URL))
。首先(),
};
VAR lstYT = query.ToList();
I want to parse atom feeds of youtube channel. Here is the link of that rss atom feeds.
http://gdata.youtube.com/feeds/api/users/cokestudio/uploads?orderby=updated
List<YTFeeds> lstYT = new List<YTFeeds>(); XDocument xDocumentYT = XDocument.Load(Server.MapPath("XMLFile.xml")); XNamespace xmlns = "http://www.w3.org/2005/Atom"; lstYT.AddRange((from entry in xDocumentYT.Descendants(xmlns + "entry").Elements(xmlns + "media:group") select new YTFeeds { Title = entry.Element(xmlns + "media:title").Value, Description = entry.Element(xmlns + "media:description").Value, Video = entry.Elements(xmlns + "media:player").ElementAt(1).Attribute("url").Value, Image = entry.Elements(xmlns + "media:thumbnail").ElementAt(1).Attribute("url").Value }).ToList());
I am getting an error which says
invalid character or hexcode ":"
. I want to get elements from tag:<media:group>
Please suggest.解决方案When writing out the name of an element using a namespace, you have to leave out the prefix, it will be handled for you. And in this case, you need a separate namespace instance to be able to get the
media
elements. So accessing the title, description, etc. should be like this:var doc = XDocument.Load(Server.MapPath(@"XMLFile.xml")); XNamespace xmlns = "http://www.w3.org/2005/Atom"; XNamespace media = "http://search.yahoo.com/mrss/"; var query = from entry in doc.Root.Elements(xmlns + "entry") let grp = entry.Element(media + "group") select new YTFeeds { Title = (string)grp.Element(media + "title"), Description = (string)grp.Element(media + "description"), Video = (string)grp.Element(media + "player").Attribute("url"), Image = grp.Elements(media + "thumbnail") .Select(e => (string)e.Attribute("url")) .First(), }; var lstYT = query.ToList();
这篇关于解析YouTube的使用LINQ供稿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!