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 look like:
<?xml version="1.0" encoding="utf-8"?>
<Root>
<Orders>
<OrderID>10248</OrderID>
<CustomerID>VINET</CustomerID>
<EmployeeID>5</EmployeeID>
<OrderDate>1996-07-04T00:00:00</OrderDate>
<RequiredDate>1996-08-01T00:00:00</RequiredDate>
<ShippedDate>1996-07-16T00:00:00</ShippedDate>
<ShipVia>3</ShipVia>
<Freight>32.3800</Freight>
<ShipName>Vins et alcools Chevalier</ShipName>
<ShipAddress>59 rue de l'Abbaye</ShipAddress>
<ShipCity>Reims</ShipCity>
<ShipPostalCode>51100</ShipPostalCode>
<ShipCountry>France</ShipCountry>
</Orders>
</Root>
here i am posting code wich fetch 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 <book> elements
for( int i = 0; i < N; ++i )
{
reader.Skip();
reader.ReadToNextSibling( "book" );
}
// read M <book> 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.