Click here to Skip to main content
13,557,707 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


69 bookmarked
Posted 22 Feb 2011
Licenced CPOL

Quick XML Reader

, 23 Feb 2011
Rate this:
Please Sign up or sign in to vote.
A quick XML interpreter for large XML files.


For my job at Trezorix, we're required to quite often open and read large (100 MB+) XML files. Usually we open XML files in Notepad, Internet Explorer (IE), or some kind of text editor. However, when you want to open a large XML file, it takes these systems hours to open the file, if these systems do not crash while opening the file. Since we work with huge XML files and we want to be able to view the content of those files reasonably quickly, we decided to take a peek on the web for existing software. We could not find a system that covers our needs and thus decided to develop a tool ourselves.


The main goal of the tool is to read large XML files and quickly present it to the screen. Most tools reading XML (except for Notepad) first read the entire file and then use an interpreter to put the XML document's structure together. We found that's the weakness of these tools because they need to read the entire XML file before they can display anything. We decided we wanted to run through the document and display data as quickly as possible, and thus developed an on-the-fly interpreter. This interpreter may not be as seamless as you're used to, but the gained performance (in my opinion) weighs much heavier.


Although tools like IE are not really capable of opening large XML files, they do have one large pro, the presentation. Because the XML files are fully interpreted, the opening and closing tags in the XML files are matched and IE will allow you to expand and collapse elements, which makes reading the XML data easier and prettier. Second is highlighting the XML content so the user is able to quickly identify elements, attributes, and values. Because of performance reasons, we decided to drop the ability to expand and collapse elements. For highlighting the XML, we decided to make use of RTF.

The code

The code is basic, simple, and to the point. We developed two classes, one for reading and interpreting the XML, and one containing the ability to search through the read XML data. A third class make these two classes come together. Both the reading and searching methods are implemented asynchronously. For reading the XML, a simple while loop does the trick.

using (FileStream streamSource = new FileStream(m_sFilename,
FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
    using (XmlReader xmlReader = XmlReader.Create(streamSource))
        StringBuilder sbMarkedUp = new StringBuilder();
        while (xmlReader.Read())
            // Write content as RTF depening on the xmlReader.NodeType

The interpreter

The interpreter is very simple. It checks the NodeType and handles the XML accordingly. If an element was found, it will write the element tag to a StringBuilder object. Each line of XML will be written to a generic list of strings. The interpreter decides when to write a line of XML to the list. After the line is added to the generic list, the StringBuilder is cleared and the process repeats itself until the while loop is finished.

Reading portions of XML

The reading class exposes a function called ReadFragment. This function accepts a parameter (Offset) allowing the user to decide where to start the reading. The ReadFragment adds a header line with RTF definitions. Then it starts adding the lines of XML from the generic string list. The property VisibleLines allows the user to define the amount of lines returned by the ReadFragment function.


The reader class exposes four events: StartParsing, EndParsing, ErrorOccured, and ReadyForPresentation which can be used in the GUI. Start- and EndParsing are used to indicate that the process reading the XML file was started or ended. The ErrorOccured event will obviously be raised when reading a file failed for whatever reason. The ReadyForPresentation event is raised when a certain amount of lines is added to the generic list. Handling this event allows you to immediately display interpreted XML to the user.


The search function is implemented to be able to find phrases within the XML document. It loops through each line in the generic list of strings and looks for the given phrase in each line. When a match is found, an event FoundItem will be raised. The matching word and line number will be returned in the event arguments. The search class will also maintain a list of found items also containing the matching words and line numbers. If the search process completes, a SearchComplete event will be raised.

for (int iCount = 0; iCount < iLines; iCount++)
    string stringToSearch = m_lstLinesToSearch[iCount];
    int foundIndex = stringToSearch.IndexOf(m_sSearchString,
    if (foundIndex >= 0)
        AddSearchResult(foundPhrase, iCount + 1);

Future plans

We plan to further develop the software so it supports a Find & Replace method and allows to save changes made by the user in the XML files. We also plan to add the ability to collapse and expand elements.


The demo project uses the DockPanel suite ( to be able to dock windows.


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


About the Author

Eduard Keilholz
Software Developer (Senior)
Netherlands Netherlands
In 1998 I started as webdesigner programming websites in Perl and later PHP. After two years wrote most of the websites in ASP and from then on lost the feeling with a linux/unix platform.

Since 2001 interested in Windows applications and now writing software using mostly C# for about 7 years now.

You may also be interested in...

Comments and Discussions

GeneralMy vote of 2 Pin
Eliezer Gensburger30-Jan-12 9:07
memberEliezer Gensburger30-Jan-12 9:07 
GeneralRe: My vote of 2 Pin
Eduard Keilholz9-Oct-12 21:25
memberEduard Keilholz9-Oct-12 21:25 
QuestionProblem with very large xml Pin
Member 40403957-Nov-11 12:22
memberMember 40403957-Nov-11 12:22 
QuestionProject won't open due to source controls Pin
Member 28774026-Aug-11 7:09
memberMember 28774026-Aug-11 7:09 
GeneralMy vote of 5 Pin
BryanWilkins1-Mar-11 2:41
memberBryanWilkins1-Mar-11 2:41 
GeneralRe: My vote of 5 Pin
Eduard Keilholz1-Mar-11 12:14
memberEduard Keilholz1-Mar-11 12:14 
GeneralMy vote of 5 Pin
Monjurul Habib28-Feb-11 20:15
memberMonjurul Habib28-Feb-11 20:15 
GeneralRe: My vote of 5 Pin
Eduard Keilholz1-Mar-11 12:14
memberEduard Keilholz1-Mar-11 12:14 
Generallike it - have 5 Pin
Pranay Rana24-Feb-11 1:17
memberPranay Rana24-Feb-11 1:17 
GeneralRe: like it - have 5 Pin
Eduard Keilholz25-Feb-11 4:52
memberEduard Keilholz25-Feb-11 4:52 
GeneralAssuming your motivation is only that described in the Introduction paragraph.... Pin
damnedyankee24-Feb-11 0:37
memberdamnedyankee24-Feb-11 0:37 
GeneralRe: Assuming your motivation is only that described in the Introduction paragraph.... Pin
bilo8124-Feb-11 5:58
memberbilo8124-Feb-11 5:58 
QuestionIf I am not wrong u can use xml Linq for search method instead of for loop? Pin
R&D_Man22-Feb-11 7:36
memberR&D_Man22-Feb-11 7:36 
AnswerRe: If I am not wrong u can use xml Linq for search method instead of for loop? Pin
Eduard Keilholz22-Feb-11 20:57
memberEduard Keilholz22-Feb-11 20:57 
GeneralLooks cool Pin
Sacha Barber22-Feb-11 6:06
mvpSacha Barber22-Feb-11 6:06 
GeneralRe: Looks cool Pin
Eduard Keilholz22-Feb-11 20:56
memberEduard Keilholz22-Feb-11 20:56 
GeneralRe: Looks cool Pin
Sacha Barber22-Feb-11 21:29
mvpSacha Barber22-Feb-11 21:29 
GeneralRe: Looks cool Pin
Eduard Keilholz17-Feb-12 4:12
memberEduard Keilholz17-Feb-12 4:12 
GeneralRe: Looks cool Pin
Sacha Barber17-Feb-12 5:03
mvpSacha Barber17-Feb-12 5:03 
GeneralSeems Good Pin
DaveKerr22-Feb-11 5:09
memberDaveKerr22-Feb-11 5:09 
GeneralRe: Seems Good Pin
Eduard Keilholz22-Feb-11 5:32
memberEduard Keilholz22-Feb-11 5:32 
GeneralRe: Seems Good Pin
John Simmons / outlaw programmer23-Feb-11 4:55
mvpJohn Simmons / outlaw programmer23-Feb-11 4:55 
GeneralRe: Seems Good Pin
Eduard Keilholz23-Feb-11 20:26
memberEduard Keilholz23-Feb-11 20:26 

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.180515.1 | Last Updated 24 Feb 2011
Article Copyright 2011 by Eduard Keilholz
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid