Open large xml files without loading the whole xml file in memory (more than 2 GB)
When we think of working with any XML file, we normally think of using XML Document (DOM) or DataSet.LoadXML(). Some programmers also use XPathDocument to load the xml file. When we use the above objects, 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. .NET framework is provided with XmlTextReader class. XmlTextReader helps us processing 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.
Below is an example of how to use XMLTextReader class: –
XmlTextReader AgencyTextReader = new XmlTextReader(filename);
AgencyTextReader.WhitespaceHandling = WhitespaceHandling.None;
if (AgencyTextReader.NodeType == XmlNodeType.Element &&
AgencyTextReader.LocalName == "award" &&
AgencyTextReader.IsStartElement() == true)
private void ProcessAwardNode(XmlTextReader AwardReader)
XmlDocument AwardXmlDoc = new XmlDocument();
// we can use xpath as below