0
Answer

Slow XML file parsing

Sergey K

Sergey K

10y
2.1k
1
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();
}
}