Read large XML files in c# with a small memory footprint

My go to class when dealing with XML is XDocument which is really friendly to use, but not memory friendly when the file you are processing is very large. I use the method below to read the file with XmlReader and turn each node I want into an XElement for further processing. Please not that the method doesn't do any error checking.

public class XmlHelper 
{
    public static IEnumerable<XElement> StreamXElements(Stream xmlStream, string elementName) {

        using var reader = XmlReader.Create(xmlStream);

        reader.MoveToContent();
        while (reader.Read())
            if (reader.NodeType == XmlNodeType.Element && reader.Name == elementName)
                yield return XElement.ReadFrom(reader) as XElement;
    }
}

Example usage:

public static void Main()
{
    using var xmlStream = File.OpenRead("/path/to/file.xml");
    foreach (var element in XmlHelper.StreamXElements(xmlStream, "nameOfElementHere"))
    {
        // process element
    }
}

This isn't a great solution if your xml file contains lots of different elements, but in my situation the outline of the documents I need to process look roughly like this:

<root>
    <property></property>
    <property></property>
    <property></property>
</root>

And use it like so:

foreach (var propertyElement in XmlHelper.StreamXElements(xmlStream, "property"))
{
    // process propertyElement
}

Back to homepage