简体   繁体   中英

Get linq to xml descendants Elements with specific attribute value

From a twitter atom feed I'm trying to get some feed data using linqToXml:

atomFeed = XDocument.Load(feedUrl);  
var tweets = (from entry in atomFeed.Descendants("entry")
                         select new
                         {
                             Date = entry.Element("published").Value,
                             Title = entry.Element("title").Value,
                             Url = entry.Element("link").Value // with type="image/jpeg"
                         }
                        );

Since there are two link types (one with attribute type="text/html" and one with type="image/jpeg" it doesn't work. I only need the link with the jpeg, but no clue how to extract only that link from the xml

Provided there is exactly one link with type "image/jpeg" (and it looks that way from the samples):

Url = entry.Elements("link")
           .Single(x => (string)x.Attribute("type") == "image/jpeg")
           .Value;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM