Best method to parse a very large XML Doc

B

Brian

Hi all,

I have an xml document that can contain 248 nodes with
each node containing different fields. There is the
possibility that there will be more nodes. Each node can
have up to 30 elements.

What do you suggest is the best way to parse through this
document? Should I create a class and deserialize the
xml into it? Should I create a giant parsing routine?
etc...

Thank everyone in advance for their answers!

Brian
 
N

Nicholas Paldino [.NET/C# MVP]

Brian,

Depending on what you are trying to do, your best bet might be to use a
class that derives from XmlReader. This will allow you to read the contents
in a streaming manner and will be much more efficient than loading a large
file like this into memory. However, it might not suit your needs. If you
need to access this information over and over again, then storing it in a
class (through XML serialization, like you mentioned) might be the better
route for you.

Hope this helps.
 
J

Jason Smith

No offense, but your XML document doesn't sound all that large. If the XML
file is less than 1MB in size, just use an XmlDocument and pull your data
out as you need it using XPath. If you haven't worked with it before, XPath
is a pretty decent query language that returns multiple nodes from anywhere
in the document. This is just about the most powerful method of getting
things out of an XML document available, if the document isn't too big to
fit in memory neatly as an XmlDocument.

If the document is big - you'll have to decide what "big" means - you can
use XmlReader, but the programming will be a lot more difficult. XmlReader
is a one-pass reader. There is no backtracking, no hierarchy, no state
(other than the state managed by the code you write). You'll have to pull
out the information you need as you parse and store it in a convenient
format. If the document is 10MB or bigger, you'll probably have to do
things the hard way.

If you can possibly work with XmlDocument, do so. Your code will be much
simpler and easier to maintain.
 

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