且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

解析***的使用LINQ供稿

更新时间:2022-10-22 22:57:17

在使用一个命名空间写出元素的名称,你要离开了前缀,它会为您处理。在这种情况下,你需要一个单独的命名空间的实例才能够获得传媒元素。所以访问标题,描述等,应该是这样的:

  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 *** channel. Here is the link of that rss atom feeds.

http://gdata.***.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();