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 Document

XmlDocument 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.

[8326 byte] By [huabing78] at [2007-12-25]
# 1

Currently the time taken to process the document will be proportional to the number of nodes in the document. There isn't really a lot you can do about this if you want to load the whole document, however you could use a "load on demand" strategy, i.e. initially just load the root node and its immediate children, and then when one of those child nodes is expanded you could just load the next level down.

GregBeech at 2007-9-3 > top of Msdn Tech,.NET Development,XML and the .NET Framework...
# 2

thanks for your comment, Greg.

will give it a shot!

huabing78 at 2007-9-3 > top of Msdn Tech,.NET Development,XML and the .NET Framework...
# 3
How to load XML on demand as mentioned? thanks
huabing78 at 2007-9-3 > top of Msdn Tech,.NET Development,XML and the .NET Framework...
# 4
up
huabing78 at 2007-9-3 > top of Msdn Tech,.NET Development,XML and the .NET Framework...
# 5

You caching your data twice. Once when you load them to XmlDocument, second time as the TreeNodeCollection .

Performance of this has nothing to do with XmlReader.

You can avoid double caching if you decide really use XmlReader and generate TreeNodeCollection "on the fly".

SergeyDubinets-MSFT at 2007-9-3 > top of Msdn Tech,.NET Development,XML and the .NET Framework...

.NET Development

Site Classified