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.
First off you should really switch to a SAX-like approach when dealing with large files. Refer to [What is the best way to parse (big) XML in C# Code?](https://stackoverflow.com/questions/676274/what-is-the-best-way-to-parse-big-xml-in-c-sharp-code) for one possible solution.

Now you have a solid way to read files that won't cause your system to crash no matter how big the file is.

Next step is to fill your treeview, as other as stated you'll need to do this in a separate thread, I'd recommend a [backgroundworker](http://msdn.microsoft.com/en-us/library/cc221403%28v=vs.95%29.aspx) as they're very easy to work with.

As for updating the UI when changes happen, I'm not familiar with winforms but if it support [ObservableCollection](http://msdn.microsoft.com/en-us/library/ms748365.aspx) I'd recommend using thous.