I recently had a project where I needed to ingest large XML documents using C# so I was curious which XML reader technology would be the fastest. So I coded up a quick benchmark that would compare LINQ to XML, XmlDocument.Load, and XmlReader against each other.
The Test Data
I generated a very simple XML file before each run of a test. The id's were random and the number of "child" nodes varied based on the run. The following is an example of the test data I used.
<root>
<child id='123'/>
<child id='234'/>
...
</root>
The Test
As I said before I wanted to compare LINQ to XML, XmlDocument.Load, and XmlReader against each other. I ran each of these technologies using 1, 10, 100, 1000, 10,000, 100,000 "child" nodes. I also ran each against a XML document using UTF-8, ASCII, and UTF-32 encodings. Each iteration was run 100 times to reduce anomalies. In each of the tests I call the method "ProcessId" which simulates the processing of the "id" attribute.
XmlDocument.Load
I thought the code for XmlDocument.Load was the cleanest and easiest to understand, although I must admit I like XPath. XmlDocument does have some security concerns but that's another post. Here is the code I used to load and search the document:
private static void XmlDocumentReader(string fileName) {
XmlDocument doc = new XmlDocument();
doc.Load(fileName);
XmlNodeList nodes = doc.SelectNodes("//child");
if (nodes == null) {
throw new ApplicationException("invalid data");
}
foreach (XmlNode node in nodes) {
string id = node.Attributes["id"].Value;
ProcessId(id);
}
}
LINQ to XML
LINQ to XML was also very easy to read and understand code. I did find that even though LINQ to XML is supposed to use XmlReaders under the covers calling XDocument.Load does read the whole document into memory before returning. So if you are looking for data at the top of middle of a very large document this could be a concern. Here is the code I used to load and search the document:
private static void XDocumentReader(string fileName) {
XDocument doc = XDocument.Load(fileName);
if (doc == null | doc.Root == null) {
throw new ApplicationException("invalid data");
}
foreach (XElement child in doc.Root.Elements("child")) {
XAttribute attr = child.Attribute("id");
if (attr == null) {
throw new ApplicationException("invalid data");
}
string id = attr.Value;
ProcessId(id);
}
}
XmlReader
XmlReader, specifically XmlTextReader was the hardest to write and understand. With it's quirks of being a forward only reader you need to take what you need while you have it because you can't rewind.
private static void XmlReaderReader(string fileName) {
using (XmlReader reader = new XmlTextReader(fileName)) {
while (reader.Read()) {
if (reader.NodeType == XmlNodeType.Element) {
if (reader.Name == "child") {
reader.MoveToAttribute("id");
string id = reader.Value;
ProcessId(id);
}
}
}
}
}
The Results
The following results are in milliseconds for each run. I took the total time to run and divided it by 100.
UTF8Encoding
| 1 | 10 | 100 | 1,000 | 10,000 | 100,000 |
|---|
| XmlDocument | 0.1567800 | 0.1713450 | 0.3888620 | 1.9816480 | 22.8049260 | 459.8570340 |
|---|
| XmlReader | 0.1467460 | 0.1439580 | 0.2300500 | 0.8534400 | 7.5771640 | 76.8635690 |
|---|
| LINQ to XML | 0.1499530 | 0.1500640 | 0.2778720 | 1.4616730 | 15.7719020 | 208.9360300 |
|---|
ASCIIEncoding
| 1 | 10 | 100 | 1,000 | 10,000 | 100,000 |
|---|
| XmlDocument | 0.1659350 | 0.1922080 | 0.3433140 | 1.9846330 | 22.5484690 | 482.8699720 |
|---|
| XmlReader | 0.1376840 | 0.1453730 | 0.2199810 | 0.8768260 | 7.9187380 | 77.7760560 |
|---|
| LINQ to XML | 0.1345900 | 0.1573340 | 0.2848420 | 1.4889930 | 15.1504500 | 214.9338990 |
|---|
UTF32Encoding
| 1 | 10 | 100 | 1,000 | 10,000 | 100,000 |
|---|
| XmlDocument | 0.1672370 | 0.1799780 | 0.4156250 | 2.7188370 | 30.6423960 | 543.4604540 |
|---|
| XmlReader | 0.1386820 | 0.1503870 | 0.2867400 | 1.4981070 | 14.4428430 | 152.7660780 |
|---|
| LINQ to XML | 0.1317060 | 0.1866610 | 0.5385940 | 2.3631290 | 21.4566290 | 274.3280280 |
|---|
Conclusion
XmlReader beats LINQ to XML in almost every run except for very small XML documents. What's interesting is how the numbers scale between the encodings. XmlReader is over twice as slow when reading UTF-32 documents verse UTF-8 or ASCII encoded XML, yet LINQ to XML and XmlDocument slowed down by a much smaller amount.
If you need speed when reading XML documents stick with XmlReader. If you need readability and maintainability of your code go with LINQ to SQL or XmlDocument.
Very interesting article.
Regarding your comment about utf32 encoding.
The percentage change of the numbers don't make sense, but if you look at the absolute change, the numbers are much closer.
For instance. For 100,000 nodes, comparing ascii to utf32.
XmlDocument -> 61s difference
XmlReader -> 75s difference
LINQ to XML -> 60s difference
I'm guessing that XmlReader spends most of it's time reading the document(which is 4 times larger), as opposed to processing the document.
You can make the LINQ to XML code substantially simpler. Two lines should do it:
var ids = XElement.Load(fileName).Elements("child").Attributes("id");
foreach (XAttribute a in ids) ProcessId((string)id);
It is not surprising the XmlReader code is faster since both the XML DOM (XmlDocument) and LINQ to XML use XmlReader to do their reading.
Anders
what is the fast way to generate an xml document?
from say web service?
regards
james
Didn't you forget about XPathDocument?