Loading an XML File And update the progress bar

  • Thread starter Thread starter RC
  • Start date Start date
R

RC

I now want to load a large XML file.
How to show the updating progress on the progress bar?
The difficulty is how can I get the amount of the file being loaded.

XmlDocument doc = new XmlDocument();
doc.load(data.xml); //<---it takes long time to load

Any sample code or solution provided?
 
When facing a similar problem I found that one way to provide visual
feedback was to use a little trick. We know that the xml parser loads
document sequentially, so that all we need is to find out which position of
the document is being requested right now. We do this by deriving our own
class from a stream and feeding it to the XmlDocument.Load. Our class simply
forwards every request to the base class, but makes sure to raise an event
on every Read operation, passing to the handler the current stream position.

A sample is available at
http://www.alexfeinman.com/download.asp?doc=LoadXmlWithProgress.zip

As pointed out in the source code comments, if you need to load xml from a
string in memory which did not originate as a file or network stream (very
unlikely), you can do a similar thing with the TextReader class
 
Thanks....it works like a charm.

Alex Feinman said:
When facing a similar problem I found that one way to provide visual
feedback was to use a little trick. We know that the xml parser loads
document sequentially, so that all we need is to find out which position of
the document is being requested right now. We do this by deriving our own
class from a stream and feeding it to the XmlDocument.Load. Our class simply
forwards every request to the base class, but makes sure to raise an event
on every Read operation, passing to the handler the current stream position.

A sample is available at
http://www.alexfeinman.com/download.asp?doc=LoadXmlWithProgress.zip

As pointed out in the source code comments, if you need to load xml from a
string in memory which did not originate as a file or network stream (very
unlikely), you can do a similar thing with the TextReader class
 
Back
Top