Sergey K

Sergey K

  • NA
  • 1
  • 2.6k

Slow XML file parsing

Nov 10 2013 11:41 PM
I am using this structure to parse XML file ( ~2 Mb)
It takes about 5 minutes to create TreeView ..  How to make it faster?



private void button1_Click(object sender, EventArgs e)
{
var doc = new XmlDocument();
  doc.Load(@"filename");
treeView1.Nodes.Clear();

var rootNode = new TreeNode(doc.DocumentElement.Name);

treeView1.Nodes.Add(rootNode);

BuildNode(doc.DocumentElement, rootNode);

treeView1.ExpandAll();
}

private void BuildNode(XmlNode xmlNode, TreeNode node)
{
if (xmlNode.HasChildNodes)
{
foreach (XmlNode childNode in xmlNode.ChildNodes)
{
var treeNode = new TreeNode(childNode.Name);
node.Nodes.Add(treeNode);
BuildNode(childNode, treeNode);
}
}
else
{
node.Text = (xmlNode.OuterXml).Trim();
}
}