从xml文档中提取最后10个元素

时间:2012-12-25 08:55:57

标签: c# linq windows-phone-7 linq-to-xml

我正在尝试从我的xml文档中提取最后10个元素,我使用此代码来解析它:

slideView.ItemsSource = 
    from channel in xmlItems.Descendants("album") 
    let id = channel.Element("catid")
    let tit = channel.Element("name")
    let des = channel.Element("picture")
    orderby (int) id descending 
    select new onair
    {
        title = tit == null ? null : tit.Value,
        photo = des == null ? null : des.Value,
    };
请帮忙:) 感谢

1 个答案:

答案 0 :(得分:0)

此代码将返回catid排序的最后10个元素:

slideView.ItemsSource = 
    xmlItems.Descendants("album") 
            .OrderByDescending(channel => (int)channel.Element("catid"))
            .Select(channel => new onair
            {
                title = (string)channel.Element("name"),
                photo = (string)channel.Element("picture")
            }).Take(10);

与查询语法相同(好吧,几乎是查询语法 - Take运算符没有关键字):

slideView.ItemsSource = 
    (from channel in xmlItems.Descendants("album")     
     orderby (int)channel.Element("catid") descending 
     select new onair
     {
         title = (string)channel.Element("name"),
         photo = (string)channel.Element("picture")
     }).Take(10);