【问题标题】:Parse youtube feeds with linq使用 linq 解析 youtube 提要
【发布时间】:2012-07-02 07:16:29
【问题描述】:

我想解析 youtube 频道的 atom 提要。 这是那个 rss atom 提要的链接。

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());

我收到一条错误消息,上面写着invalid character or hexcode ":"。我想从标签中获取元素:&lt;media:group&gt; 请提出建议。

【问题讨论】:

    标签: c# xml linq youtube feed


    【解决方案1】:

    当使用命名空间写出元素的名称时,您必须省略前缀,它将为您处理。在这种情况下,您需要一个单独的命名空间实例才能获取 media 元素。所以访问标题、描述等应该是这样的:

    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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-03-27
      • 2012-10-19
      • 1970-01-01
      • 1970-01-01
      • 2011-03-10
      • 2011-04-30
      • 2013-03-09
      • 2012-02-13
      相关资源
      最近更新 更多