Loading an XML File And update the progress bar

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?
 
A

Alex Feinman [MVP]

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
 
R

RC

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top