【问题标题】:extract last 10 elements from xml document从 xml 文档中提取最后 10 个元素
【发布时间】:2012-12-25 17:17:56
【问题描述】:

我正在尝试从我的 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,
    };

请帮忙:) 谢谢

【问题讨论】:

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


    【解决方案1】:

    此代码将返回按 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);
    

    【讨论】:

    • 谢谢,效果很好,但我正在尝试添加一个按钮,如果用户点击它,它会加载接下来的 10 个元素,因为我在 xml 上有很多元素:/ 有什么想法吗?跨度>
    • @MeriemBenMrad 你可以使用Skip(pageIndex*pageSize).Take(pageSize)
    • 你能解释更多并给我一个例子吗?
    • @MeriemBenMrad google for paging with LINQ
    • 我不认为我可以在这里处理分页,因为我从第一个请求中得到了洞 xml 文档。所以有什么方法可以在每次单击时提取接下来的 10 个元素?谢谢
    猜你喜欢
    • 1970-01-01
    • 2013-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-03
    相关资源
    最近更新 更多