|
|
performence issue of an XML reader |
|
Author |
Message |
huabing78
|
Posted: XML and the .NET Framework, performence issue of an XML reader |
Top |
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
{
public partial class XMLReader_WindowsBased : Form
{
public XMLReader_WindowsBased()
{
InitializeComponent();
}
private void btnSelect_Click(object sender, EventArgs e)
{
ofdXMLFile.ShowDialog();
txtXmlFile.Text = ofdXMLFile.FileName;
}
private void cmdLoad_Click(object sender, EventArgs e)
{
// Clear the tree.
treeXml.Nodes.Clear();
// Load the XML Document
XmlDocument doc = new XmlDocument();
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();
}
private void 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)
{
case XmlNodeType.ProcessingInstruction:
case XmlNodeType.XmlDeclaration:
newTreeNode.Text = "<" + xmlNode.Name + " " + xmlNode.Value + ">";
break;
case XmlNodeType.Element:
newTreeNode.Text = "<" + xmlNode.Name + ">";
break;
case XmlNodeType.Attribute:
newTreeNode.Text = "ATTRIBUTE: " + xmlNode.Name;
break;
case XmlNodeType.Text:
case XmlNodeType.CDATA:
newTreeNode.Text = xmlNode.Value;
break;
case XmlNodeType.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 attribute in 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 childNode in xmlNode.ChildNodes)
{
ConvertXmlNodeToTreeNode(childNode, newTreeNode.Nodes);
}
}
private void 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.
.NET Development11
|
|
|
|
|
Greg Beech
|
Posted: XML and the .NET Framework, performence issue of an XML reader |
Top |
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.
|
|
|
|
|
Sih Huseyin Ulger
|
Posted: XML and the .NET Framework, performence issue of an XML reader |
Top |
|
|
|
huabing78
|
Posted: XML and the .NET Framework, performence issue of an XML reader |
Top |
can you give some details on how to apply XmlReader in my case Thanks
|
|
|
|
|
huabing78
|
Posted: XML and the .NET Framework, performence issue of an XML reader |
Top |
thanks for your comment, Greg.
will give it a shot!
|
|
|
|
|
huabing78
|
Posted: XML and the .NET Framework, performence issue of an XML reader |
Top |
How to load XML on demand as mentioned thanks
|
|
|
|
|
Sih Huseyin Ulger
|
Posted: XML and the .NET Framework, performence issue of an XML reader |
Top |
You can use code sample at http://msdn2.microsoft.com/en-us/library/system.xml.xmlreader.nodetype.aspx to walk xml (once) using XmlReader. All you need to do is special case Element nodes. You can keep a stack of your TreeNodes and on each XmlNodeType.Element you need to push the current TreeNode to the stack and create a new TreeNode, on XmlNodeType.EndElement you need to pop TreeNode from the stack and make it current TreeNode. Also create/initialize apropriate TreeNodes and add them to current TreeNode on each node type.
Let me know if this does not help.
|
|
|
|
|
huabing78
|
Posted: XML and the .NET Framework, performence issue of an XML reader |
Top |
|
|
|
Sergey Dubinets - MSFT
|
Posted: XML and the .NET Framework, performence issue of an XML reader |
Top |
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".
|
|
|
|
|
|
|