performence issue of an XML reader
Codes:
// Load an XML file and display the structure and content in a tree view.
using
System;using
System.Collections.Generic;using
System.ComponentModel;using
System.Data;using
System.Drawing;using
System.Text;using
System.Windows.Forms;using
System.Xml;namespace
XMLReader{
publicpartialclassXMLReader_WindowsBased :Form{
public XMLReader_WindowsBased(){
InitializeComponent();
}
privatevoid btnSelect_Click(object sender,EventArgs e){
ofdXMLFile.ShowDialog();
txtXmlFile.Text = ofdXMLFile.FileName;
}
privatevoid cmdLoad_Click(object sender,EventArgs e){
// Clear the tree.treeXml.Nodes.Clear();
// Load the XML DocumentXmlDocument doc =newXmlDocument();try{
doc.Load(txtXmlFile.Text);
}
catch (Exception){
MessageBox.Show(" Error: The selected or entered XML file was not found! ");return;}
// Populate the TreeView.ConvertXmlNodeToTreeNode(doc, treeXml.Nodes);
// Expand all nodes.treeXml.Nodes[0].ExpandAll();
}
privatevoid ConvertXmlNodeToTreeNode(XmlNode xmlNode,TreeNodeCollection treeNodes){
// Add a TreeNode node that represents this XmlNode.TreeNode newTreeNode = treeNodes.Add(xmlNode.Name);// Customize the TreeNode text based on the XmlNode// type and content.switch (xmlNode.NodeType){
caseXmlNodeType.ProcessingInstruction:caseXmlNodeType.XmlDeclaration:newTreeNode.Text =
"<" + xmlNode.Name +" " + xmlNode.Value +">";break;caseXmlNodeType.Element:newTreeNode.Text =
"<" + xmlNode.Name +">";break;caseXmlNodeType.Attribute:newTreeNode.Text =
"ATTRIBUTE: " + xmlNode.Name;break;caseXmlNodeType.Text:caseXmlNodeType.CDATA:newTreeNode.Text = xmlNode.Value;
break;caseXmlNodeType.Comment:newTreeNode.Text =
"<!--" + xmlNode.Value +"-->";break;}
// Call this routine recursively for each attribute.// (XmlAttribute is a subclass of XmlNode.)if (xmlNode.Attributes !=null){
foreach (XmlAttribute attributein xmlNode.Attributes){
ConvertXmlNodeToTreeNode(attribute, newTreeNode.Nodes);
}
}
// Call this routine recursively for each child node.// Typically, this child node represents a nested element,// or element content.foreach (XmlNode childNodein xmlNode.ChildNodes){
ConvertXmlNodeToTreeNode(childNode, newTreeNode.Nodes);
}
}
privatevoid label2_Click(object sender,EventArgs e){
}
}
}
Issues:
It's ok to open small XML files. But for large XML files, it's fairly slow and loses response.
How should I enhance the codes to run fast? Is there any better solution?
Thanks.

