Click here to Skip to main content
14,357,666 members

Large XML Files Processing and Indexing

Rate this:
4.88 (10 votes)
Please Sign up or sign in to vote.
4.88 (10 votes)
14 Nov 2008CPOL
Index large XML file for fast access. Use IO and XMLReader for parsing with Regex.


Figure 9


XML document performs well if it is relatively small (less than 10-20 MB). DOM loads content into memory and does validation and so on… but what to do with large documents? 

Bigger documents use more memory and will consume more resources and performance will suffer significantly. Tool like XML Spy will try to load it into memory and try to validate the document and after all will … hang and run out of memory. (Files tested were over 250 MB). Other XML parsers will just crash silently.  


You are probably having a similar issue, otherwise you would not read this.

Here is a simple solution to get the nodes of interest or all nodes from a large-huge XML document without any performance hit (well - small one) and almost no memory or CPU hits.

This is what we do, we index our nodes of interest in memory or in this example into another indexing document (*.xml.idx) and write output nodes into another document (*.xml.sorted). We can now use indexes for fast access to any node of interest. It is up to you to generate conditions and selection methods.

This is a very general example of processing Invoices for customers. The test file was over 420MB and it took on an average 40-50 seconds to process it while writing another document with all nodes of interest. The processing time will be based on your processor and amount of memory on your box. 

Key elements: FileStream, XMLReader.ReadSubtree and Regex.

Using the Code

How to:

  1. Get source code and compile project. It was build with Visual Studio 2005 but will work in Visual Studio 2003 and Visual Studio 2008.

  2. Create an XML document with <INVOICE ID=”123”…..> elements in it. See Fig. 6 for an example. a. If you have your own large XML document you want to play with, then type node of interest into “Node to Index on” box and specify value you want to get. In my case, I am interested in INVOICE ID so my condition will be evaluated to "//./@ID" – Current Element ID attribute. Pick your own for this test with your document. 


    Figure 1

  3. Click on Index XML File to get a copy of your large XML document (Fig 2). It will be copied into directory where your Debug folder is or into the application folder depending on how you run this. Well you know it….


    Figure 2

    private void MakeCopy() {
        string curDir = Application.StartupPath + "/";
        FileInfo fi = new FileInfo(filePath);
        wokingCopy = curDir + (Guid.NewGuid()) + "_" + fi.Name;
        this.Text = "File size is:" + fi.Length/1000000 + " - MB";
        File.Copy(filePath, wokingCopy, true);
        xmlIndexFile = wokingCopy + ".idx";
        this.Text += " Copied to:" + curDir;
        using (StreamWriter f = File.CreateText(xmlIndexFile)) {
        sw = new StreamWriter(xmlIndexFile, true, Encoding.UTF8);

After copying file to the new directory, the file size will be shown in Form Text field (Figure 3).


Figure 3

Start parsing process and write to *.idx and *.sorted files.

Use Regex to find if there is a match in line.

using (FileStream fs = new FileStream(wokingCopy, FileMode.Open, FileAccess.Read))
using (StreamReader sr = new StreamReader(wokingCopy, Encoding.UTF8)) {
string parseText = txtNode.Text.Trim();
//Matching expression for the node:
Regex rx = new Regex(@"<" + parseText, RegexOptions.Compiled | RegexOptions.IgnoreCase);
int pos = 0;
int startIndex = 0;
int lastPositio = 0;
//Read each line in XML document as regular file stream.
do {
    string line = sr.ReadLine();
    pos += Encoding.UTF8.GetByteCount(line) + 2;// 2 extra bites for end of line chars.
    MatchCollection m = rx.Matches(line);
    foreach (Match mt in m) {
	startIndex = lastPositio + mt.Index;
	ValidateXPathCondition(fs, startIndex);
    lastPositio = pos;
} while (!sr.EndOfStream);



At the end, your processing time in seconds will be displayed in Form Text (Figure 4):


Figure 4

This is how you read the stream and store information:

 private void WriteSortedDocument() {
    using(FileStream fs = new FileStream(wokingCopy, FileMode.Open, FileAccess.Read))
    using (StreamWriter wr = new StreamWriter(wokingCopy + ".sorted", false)) {
	wr.WriteLine("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
	wr.WriteLine("<" + txtNode.Text.Trim().ToUpper() + "S>");
	foreach (int key in indexList.Keys) {
	    fs.Seek(indexList[key], SeekOrigin.Begin);
	    using (XmlReader reader = XmlReader.Create(fs)) {
		XmlDocument d = new XmlDocument();
	wr.WriteLine("</" + txtNode.Text.Trim().ToUpper() + "S>");

/// <summary>
/// Write value and index in original file into the indexing file.
/// </summary>
/// <param name="value"></param>
/// <param name="startIndex"></param>
private void SaveMatchedIndex(string value, int startIndex) {            
    sw.WriteLine(value + "\t" + startIndex);
    //Add new element to sorted list. Sorting is by key
    indexList.Add(Int32.Parse(value), startIndex);

New files generated:

Fig5.JPG - Click to enlarge
Figure 5

Original XML File snippet:


Figure 6

Part of index file *.xml.idx:


Figure 7

Part of sorted  *.sorted XML file: File was sorted by Invoice ID.


Figure 8

Test your indexes.
Go to *.xml.idx file and pick any index (this is the second tab) and type this value into position in copied file. The node of interest will be displayed immediately.

Click on Read button to Test Index Position 311383: It will return the selected node. See Figure 9 at the top of this article.

Points of Interest

If you have any comments or questions, please email me.
There is another great project to index large XML documents, but it has its own cons and pros.


  • Created on 14th November, 2008


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


About the Author

Slava Khristich
Software Developer (Senior) Tateeda Media Networks
United States United States

Software development is my passion as well as photography.

If you got a sec stop by to see my photography work at

Tateeda Media Network

Comments and Discussions

GeneralGreat article !! Pin
knoop20-Sep-09 23:22
memberknoop20-Sep-09 23:22 
Generalgood use of regular expression [modified] Pin
Ardavan Sharifi15-Nov-08 1:15
memberArdavan Sharifi15-Nov-08 1:15 

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.

Posted 14 Nov 2008


47 bookmarked