LINQ读取xml数据时如何进行分页

时间:2015-07-19 12:10:06

标签: c# xml linq pagination

以下方式我正在使用linq读取xml数据。现在展示如何使用分页来获取数据

XDocument document = XDocument.Load(@"c:\users\tridip\documents\visual studio 2010\Projects\WindowsFormsApplication5\WindowsFormsApplication5\Orders.xml");
            var books = from r in document.Descendants("Orders")
            select new
            {
                OrderID = r.Element("OrderID").Value,
                CustomerID = r.Element("CustomerID").Value,
                EmployeeID = r.Element("EmployeeID").Value,
            };

我有一个示例脚本,但看起来有点不同。这是代码

var limit=100;

var items = xmldoc.Descendants("whatevernodename")
           .Select(node => node.Value.ToString())
           .Skip(limit)
           .Take(100)
           .ToList();

如果看到上面的代码,你可以看到所有字段都会返回,但是这种方式我需要指定字段

select new
            {
                OrderID = r.Element("OrderID").Value,
                CustomerID = r.Element("CustomerID").Value,
                EmployeeID = r.Element("EmployeeID").Value,
            };

并告诉我如何通过订单ID ASC或DESC指定订单

感谢

2 个答案:

答案 0 :(得分:0)

您可以逐步构建LINQ-to-XML查询,例如:

//setup basic query
var query = from r in document.Descendants("Orders")
            select new
            {
                OrderID = r.Element("OrderID").Value,
                CustomerID = r.Element("CustomerID").Value,
                EmployeeID = r.Element("EmployeeID").Value,
            };

//setup query result ordering,
//assume we have variable to determine ordering mode : bool isDesc = true/false
if (isDesc) query = query.OrderByDescending(o => o.OrderID);
else query = query.OrderBy(o => o.OrderID);

//setup pagination, 
//f.e displaying result for page 2 where each page displays 100 data
var page = 2;
var pageSize = 100;
query = query.Skip(page - 1*pageSize).Take(pageSize);

//execute the query to get the actual result
var items = query.ToList();

答案 1 :(得分:0)

所以我的更新代码如下,我从@ Har07获得答案后修复了。

XDocument document = XDocument.Load(@"c:\users\documents\visual studio 2010\Projects\WindowsFormsApplication5\WindowsFormsApplication5\Orders.xml");
            bool isDesc = true;
            //setup basic query
            var query = from r in document.Descendants("Orders")
            select new
            {
                OrderID = r.Element("OrderID").Value,
                CustomerID = r.Element("CustomerID").Value,
                EmployeeID = r.Element("EmployeeID").Value,
            };

            //setup query result ordering,
            //assume we have variable to determine ordering mode : bool isDesc = true/false
            if (isDesc) query = query.OrderByDescending(o => o.OrderID);
            else query = query.OrderBy(o => o.OrderID);

            //setup pagination, 
            //f.e displaying result for page 2 where each page displays 100 data
            var page = 1;
            var pageSize = 5;
            query = query.Skip(page - 1 * pageSize).Take(pageSize);

            //execute the query to get the actual result
            //var items = query.ToList();
            dataGridView1.DataSource = query.ToList();