Working with XML files in memory is always a performance issue. It becomes more important to look into the processing of XML files which are heavy in size (let's 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:
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 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 the code snippet that shows an example of how to use
XmlTextReader myTextReader = new XmlTextReader(filename);
myTextReader.WhitespaceHandling = WhitespaceHandling.None;
if (myTextReader.NodeType == XmlNodeType.Element &&
myTextReader.LocalName == "Reward" &&
myTextReader.IsStartElement() == true)
Here is the method implementation of
private void ProcessRewardNode(XmlTextReader RewardReader)
XmlDocument RewardXmlDoc = new XmlDocument();
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 help !!!