简体   繁体   English

获取linq到xml后代具有特定属性值的元素

[英]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: twitter原子feed我试图使用linqToXml获取一些feed数据:

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 由于有两种链接类型(一种是属性类型=“text / html”,一种是type =“image / jpeg”,它不起作用。我只需要与jpeg的链接,但不知道如何仅提取从xml链接

Provided there is exactly one link with type "image/jpeg" (and it looks that way from the samples): 如果只有一个链接类型为“image / jpeg”(它从样本中看起来那样):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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