Click here to Skip to main content
12,998,712 members (78,493 online)
Click here to Skip to main content
Add your own
alternative version


19 bookmarked
Posted 13 Nov 2013

NanoXML - Simple and fast XML parser

, 13 Nov 2013
Rate this:
Please Sign up or sign in to vote.
Simple and fast .NET XML parser without using System.Xml


This is a quite small and fast XML DOM parser on the .NET platform (using .NET 2.0). The Main feature and main demand for this is not to use the System.Xml namespace. Also tests shows amazing performance results compared with built-in .NET parsers.


The idea of such a thing was born when one of my friends needed something to parse XML on C# without using the System.Xml namespace. The writing and testing took about three hours. The parser doesn't support the entire XML specification but it is capable of parsing most XML I've tried.

Using the code

Usage of NanoXML is simple. You just need to add NanoXMLParser.cs to your project and use the TObject.Shared namespace. The main top-level class is NanoXMLDocument that parses XML and builds DOM.

NanoXML is not capable of loading data from files, so you may load an XML string with your application itself. Something like this: 

FileStream fs = new FileStream(args[0], FileMode.Open, FileAccess.Read);
byte[] data = new byte[fs.Length];
fs.Read(data, 0, (int) fs.Length);
string strData = Encoding.UTF8.GetString(data);
NanoXMLDocument xml = new NanoXMLDocument(strData);

Yes, NanoXML ignores XML declaration's encoding attribute  Now, after we have loaded the document, we can get data for any Element or any of its attributes: 

string myAttribute = xml.RootNode["Subnode"].GetAttribute("myAttribute"); 

NanoXML also ignores comments and DOCTYPE declarations. XML declarations (<?xml ?>) will be parsed and stored in a NanoXMLDocument object. 


The most amazing thing in this parser is its performance. Before submitting the code here, I tried some benchmarks on the parser and compared it with built-in .NET parsers and I was surprised. All tests were performed on an 1.1 MB SVG file in string (i.e., without disk access overhead). Test results are shown below on the screenshot:


As we can see, NanoXML processes an 1.1 MB file almost immediately (17 ms). XmlDocument loaded document in 11 seconds. XmlReader (a SAX parser which by design should be much faster than DOM) reads the whole document for about 7 seconds. For the XmlReader, the test doesn't do anything but read file content from beginning to end.  Benchmark test sources are available for download. 

Points of Interest 

This parser may be useful because of its great performance or when using built-in parsers (System.Xml namespace) is forbidden.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer Broken Event
Ukraine Ukraine
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionHi Pin
AAB4022-Feb-17 2:54
memberAAB4022-Feb-17 2:54 
AnswerRe: Hi Pin
BrokenEvent24-Feb-17 7:00
memberBrokenEvent24-Feb-17 7:00 
GeneralRe: Hi Pin
AAB4027-Feb-17 1:35
memberAAB4027-Feb-17 1:35 
GeneralRe: Hi Pin
BrokenEvent2-Mar-17 9:00
memberBrokenEvent2-Mar-17 9:00 
GeneralRe: Hi Pin
AAB402-Mar-17 21:57
memberAAB402-Mar-17 21:57 
GeneralRe: Hi Pin
BrokenEvent3-Mar-17 6:40
memberBrokenEvent3-Mar-17 6:40 
GeneralRe: Hi Pin
AAB406-Mar-17 1:17
memberAAB406-Mar-17 1:17 
SuggestionMore benchmarks would be helpful Pin
E.I.B.14-Feb-17 4:28
memberE.I.B.14-Feb-17 4:28 
GeneralRe: More benchmarks would be helpful Pin
BrokenEvent19-Feb-17 8:42
memberBrokenEvent19-Feb-17 8:42 
QuestionSubNode Pin
wfm8-Jun-15 5:44
memberwfm8-Jun-15 5:44 
AnswerRe: SubNode Pin
wfm8-Jun-15 5:52
memberwfm8-Jun-15 5:52 
Questionwhere is the code? Pin
Southmountain13-Nov-13 11:29
memberSouthmountain13-Nov-13 11:29 
AnswerRe: where is the code? Pin
TObject37313-Nov-13 11:46
memberTObject37313-Nov-13 11:46 
GeneralRe: where is the code? Pin
Southmountain13-Nov-13 11:51
memberSouthmountain13-Nov-13 11:51 
GeneralRe: where is the code? Pin
Sergey Yolkin13-Nov-13 11:56
memberSergey Yolkin13-Nov-13 11:56 
GeneralRe: where is the code? Pin
TObject37313-Nov-13 12:02
memberTObject37313-Nov-13 12:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170622.1 | Last Updated 13 Nov 2013
Article Copyright 2013 by BrokenEvent
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid