How to do the pagination with XmlReader class c#
09:26 19 Jul 2015

At present I am fetching data from xml file using LINQ but the problem is I am using XDocument to load xml file but XDocument class load xml data into memory. so if there is 10,000 data in my xml file then XDocument class will load 10,000 data into memory. so some one tell me if use read xml data with XmlReader class then it will not dump full data into memory.

At present this way I am fetching data from xml file.

My xml data looks like:



  
    10248
    VINET
    5
    1996-07-04T00:00:00
    1996-08-01T00:00:00
    1996-07-16T00:00:00
    3
    32.3800
    Vins et alcools Chevalier
    59 rue de l'Abbaye
    Reims
    51100
    France
  

here I am posting code which fetches data from xml file with order by and paging.

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();

So some one tell me how could I use xmlreader to read data from xml file with pagination and order by clause will be there.

I got one hit but do not understand how to use it for my purpose:

using( var reader = XmlReader.Create( . . . ) )
{
       reader.MoveToContent();
       reader.ReadToDescendant( "book" );
       // skip N  elements
       for( int i = 0; i < N; ++i )
       {
              reader.Skip();
              reader.ReadToNextSibling( "book" );
       }
       // read M  elements
       for( int i = 0; i < M; ++i )
       {
              var s = reader.ReadOuterXml();
              Console.WriteLine( s );
              reader.ReadToNextSibling( "book" );
       }
}

So please see the above code and help me to construct the code which would use xml reader to fetch paginated data.

c# xml linq pagination xmlreader