Question: I'm creating a small tool and this tool will include a treeview of XML. There's no problem dealing with XML files with a small file size, but when I try to load a large XML file (21MB in size), my application becomes unresponsive and takes too much time to load the XML, and most of the time it doesn't load the XML at all. Is there any fix or tweak to make the below codes faster?

```
public TreeView(string filename)
        {
            InitializeComponent();

            this.Text = filename;

            XmlDataDocument xmldoc = new XmlDataDocument();
            XmlNode xmlnode;
            FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
            xmldoc.Load(fs);
            xmlnode = xmldoc.ChildNodes[1];
            treeView1.Nodes.Clear();
            treeView1.Nodes.Add(new TreeNode(xmldoc.DocumentElement.Name));
            TreeNode tNode;
            tNode = treeView1.Nodes[0];
            AddNode(xmlnode, tNode);
        }

        private void AddNode(XmlNode inXmlNode, TreeNode inTreeNode)
        {
            XmlNode xNode;
            TreeNode tNode;
            XmlNodeList nodeList;
            int i = 0;
            if (inXmlNode.HasChildNodes)
            {
                nodeList = inXmlNode.ChildNodes;
                for (i = 0; i <= nodeList.Count - 1; i++)
                {
                    xNode = inXmlNode.ChildNodes[i];
                    inTreeNode.Nodes.Add(new TreeNode(xNode.Name));
                    tNode = inTreeNode.Nodes[i];
                    AddNode(xNode, tNode);
                }
            }
            else
            {
                inTreeNode.Text = inXmlNode.InnerText.ToString();
            }
        }

```

Answer: 
You will have to separate the loading part from building the TreeView.

The loading can then happen on a background thread (BackgroundWorker).

Everything related to the TreeView has to happen on the Main thread but you can speed it up by using SuspendLayout et al.
Instead of using `XmlDataDocument`, change the code to use `XmlReader` as this class offers the highest performance when working with XML files.
XmlReader is an abstract class that can read from a file or from any other stream of data. When reading from a file, you don't load the entire document at once.

more information here: [Quick overview](http://www.hanselman.com/blog/AtAGlanceXmlReaderVsXPathNavigatorVsXmlDocument.aspx) by shanselman  

or here [Performance: LINQ to XML vs XmlDocument vs XmlReader](http://www.nearinfinity.com/blogs/joe_ferner/performance_linq_to_sql_vs.html)