How to Open Large XML files without Loading the xml Files?


Working with Xml files in Memory is always a performance issue. It become more important to look into the processing of Xml files which are heavy in size (lets say more than 3 GB). So questions comes in mind that how to process such heavy Xml files.

When we think of working with any XML file, we normally think of using

  • XMLDocument
  • DataSet.ReadXml()
  • XPathDocument

When we use the above options, we are loading the files into the system memory.

The problem is that, if the size of the xml file is for e.g. 5 GB to 7 GB, we have to load the complete file in System’s Memory. This will cost us systems memory and will throw “System out of Memory Exception”.

The best approach to process such large files is to avoid loading the files into the memory.

Microsoft has provided with XmlTextReader class. XmlTextReader helps us to process the xml file line by line. In this way we are not loading the complete xml file into the memory but processing the file line by line, node by node.

Here is code snippet that shows an example of how to use XMLTextReader class: –

XmlTextReader myTextReader = new XmlTextReader(filename);
myTextReader.WhitespaceHandling = WhitespaceHandling.None;
while (myTextReader.Read())
{
	if (myTextReader.NodeType == XmlNodeType.Element &&
		myTextReader.LocalName == "Reward" &&
		myTextReader.IsStartElement() == true)
        {
        	ProcessRewardNode(myTextReader);
                myTextReader.Skip();
	}
}

Here is method implementations of ProcessRewardNode :

private void ProcessRewardNode(XmlTextReader RewardReader)
{
	XmlDocument RewardXmlDoc = new XmlDocument();
	RewardXmlDoc.LoadXml(RewardReader.ReadOuterXml());
	// we can use xpath as below
	myID = RewardXmlDoc.SelectSingleNode("Reward/myID").InnerText;
}

Here code itself tells you lots of things, so i am not discussing it more here. you can look into MSDN of XMLTextReader for more information.

Hope this will helps !!!

Jay Ganesh

5 thoughts on “How to Open Large XML files without Loading the xml Files?

  1. I am getting a Xml File of around 1 lkah record . I need to export the Complete data to a Dataset which contains multiple data table.Kindly Explain me in detail, how to solve this out of memory Issue when I use ReadXml method from dataset.

Leave a comment