XMLDocument or XMLReader?


I have an XML file which contains 'meta data' for my application such as values for combo boxes, etc.

It's structure is like this:

      <ComboValue3 attr1="1" attr2 = "a">value1</ComboValue3>
      <ComboValue3 attr1="2" attr2 = "b">value2</ComboValue3>
      <ComboValue3 attr1="3" attr2 = "c">value3</ComboValue3>
      <ComboValue3 attr1="4" attr2 = "d">value4</ComboValue3>

The structure never goes any deeper than that,and as you can see there are attributes with some of the nodes.

This data is used all over in my application, and at the moment I am loading this file into a global XMLDocument at startup and using XPath values to retrieve the data when it's needed.

What I would like to know is if using an XMLReader would be better in terms of speed.
Most combo boxes are populated when the form they are on is loaded, but some are populated dynamically in response to user input.

Any input regarding this would be great, and if anyone could offer some guidelines about when XMLDocument or XMLReader is best to use.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

The XMLDocument for the situation you described above is a good choice. The XMLDocument class allows to you navigate through the XML repeatedly without having to reopen it.

The XMLReader provides forward-only read access to the document.

I use both types in my programming depending on the situation. Main thing to remember her is that the XMLReader is faster that the XMLDocument but it is read forward only you cant go backwards through the file. So if you need data that has already been passed you have to close and reopen to start from the beginning again.

In your description above you are accessing the XMLDocument from various points in your program so between the two the best use is the XMLDocument since you may need access to Node 3 before Node 2 the XMLDocument allows that type of navigating.

If you are only reading the values in, you could switch to the XMLReader since it will load the data faster and load the values into globally accessible variables or structures. The variables and structures would be faster to interface with than the XMLDocument.

Hope this helps

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
janineoAuthor Commented:
Thanks for explaining it to me.
I had found web sites extolling the virtues of XMLReader and was wondering if I had chosen the wrong method!
Your explanation clears it up.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.