The xml is quite simple and looks something like this:
- xml
- entry
- name
- adress
- (...)
StreamReader
For a reference, I've started off with a simple stream read:
StreamReader reader = File.OpenText("c:\\testfile2.xml");
string input = null;
while ((input = reader.ReadLine()) != null)
{
Console.WriteLine(input);
}
And the result:
watch.Elapsed = {00:00:02.3910558}
The result is pretty much as expected, about two seconds to read all of the lines an put them on the screen.
XMLReader
Next up is the xml reader. It traverses the xml straight forward and reads all of the nodes.
FileStream stream = new FileStream("c:\\testfile2.xml", FileMode.Open);
XmlReader reader = new XmlTextReader(stream);
while(reader.Read())
{
Console.WriteLine(reader.Value);
}
And the result:
watch.Elapsed = {00:00:12.7904473}
12 seconds is about as expected. There's some overhead with finding the xml nodes, but it all seem pretty much like expected. If we would try this experiment over the internet or a slower file network, I believe that the xml reader overhead would not be that visible.
XPath
Next, we try the xPath aproach:
FileStream stream = new FileStream("c:\\testfile2.xml", FileMode.Open);
XPathDocument document = new XPathDocument(stream);
XPathNavigator navigator = document.CreateNavigator();
XPathNodeIterator node = navigator.Select("xml/entry");
for(...){...}
And the result:
watch.Elapsed = {00:03:29.5681325}
I knew it would take some time, but this is not acceptable. XPath is still a kind of favourite as it makes it possible to navigate the xml tree in a absolutely beautiful way.
DataSet
Next is the dataset approach.
DataSet ds = new DataSet();
ds.ReadXml("C:\\testfile2.xml");
foreach (DataTable tbl in ds.Tables)
{
foreach (DataRow dr in tbl.Rows)
{
for (...){...}
}
}
And the result:
watch.Elapsed = {00:00:03.6352829}
I'm a bit surprised. The dataset seems like the fastest way of traversing an xml file. Note that dataset navigation can be cumbersome when containing a lot of tables, like the one.
A smaller xmlfile
I've also tried the same aproaches to a 5kb xml file, here's the results and now it turns out XPath is the fastest method:
FileStream:
watch.Elapsed = {00:00:00.0144736}
XMLReader:
watch.Elapsed = {00:00:00.0302896}
Xpath:
watch.Elapsed = {00:00:00.0151563}
Dataset:
watch.Elapsed = {00:00:00.0225523}