Introduction

XML (eXtensible Markup Language) is a widely used format for structuring and storing data in a hierarchical manner. It consists of elements enclosed in tags, which can have attributes and contain text or other elements. However, processing large XML files can be memory-intensive, especially if you load the entire document into memory at once.

Use XmlTextReader to parse large XML documents

using System.Xml;

public void FindParticularNodesUsingTextReader()
{
    string xmlFilePath = @"C:\Document and Settings\Administrator\Desktop\sampleXmlDoc.xml";

    using (XmlTextReader txtReader = new XmlTextReader(xmlFilePath))
    {
        txtReader.WhitespaceHandling = WhitespaceHandling.None;

        while (txtReader.Read())
        {
            if (txtReader.Name.Equals("TotalPrice") && txtReader.IsStartElement())
            {
                txtReader.Read();
                richTextBox1.AppendText(txtReader.Value);
            }
        }
    }
}

Output

12.36 11.99 7.97

Faster, read-only XPath query-based access to data, use XPathDocument and XPathNavigator along with xpath query.

using System.Xml.XPath;

public void FindTagsUsingXPathNavigatorAndXPathDocumentNew()
{
    string xmlFilePath = @"C:\Documents and Settings\Administrator\Desktop\sampleXmlDoc.xml";

    XPathDocument xpDoc = new XPathDocument(xmlFilePath);
    XPathNavigator xpNav = xpDoc.CreateNavigator();
    XPathExpression xpExpression = xpNav.Compile("/Orders/Order/TotalPrice");
    XPathNodeIterator xpIter = xpNav.Select(xpExpression);

    while (xpIter.MoveNext())
    {
        richTextBox1.AppendText(xpIter.Current.Value);
    }
}

Output

12.36 11.99 7.97

Combining XmlReader and XmlDocument. On the XmlReader, use the MoveToContent and Skip methods to skip unwanted items.

using System.Xml;

public void UseXmlReaderAndXmlDocument()
{
    string xmlFilePath = @"C:\Documents and Settings\Administrator\Desktop\sampleXmlDoc.xml";

    using (XmlReader rdrObj = XmlReader.Create(xmlFilePath))
    {
        while (rdrObj.Read())
        {
            if (rdrObj.NodeType.Equals(XmlNodeType.Element) &&
                rdrObj.Name.Equals("TotalPrice") &&
                rdrObj.IsStartElement())
            {
                rdrObj.Read();
                richTextBox1.AppendText(rdrObj.Value);
            }
        }
    }
}

Output

12.36 11.99 7.97

using System.Xml;

public void UseXmlReaderAndXmlDocumentNew()
{
    string xmlFilePath = @"C:\Documents and Settings\Administrator\Desktop\sampleXmlDoc.xml";

    using (XmlReader rdrObj = XmlReader.Create(xmlFilePath))
    {
        XmlDocument xmlDocObj = new XmlDocument();
        
        while (rdrObj.Read())
        {
            if (rdrObj.NodeType == XmlNodeType.Element &&
                rdrObj.Name.Equals("TotalPrice") &&
                rdrObj.IsStartElement())
            {
                rdrObj.Read();
                richTextBox1.AppendText(rdrObj.Value);
            }
        }

        rdrObj.Close(); // Close the XmlReader before loading into XmlDocument
        
        xmlDocObj.Load(xmlFilePath);
        richTextBox1.Text = xmlDocObj.InnerText;
    }
}

Design Considerations

Parsing XML

Validating XML

Writing XML

DOM Processing

XPath Processing

XSLT Processing