Solved

XML

Posted on 2004-10-03
18
402 Views
Last Modified: 2013-11-18
Hi,

I'd like to know what are the advantages/ disadvantages of XmlDocument against the XmlTextReader/ XmlTextWriter classes. For some reason, I find the API of XmlDocument more simple and easy to use than the reader/ writer.

My initial impression is that XmlDocument loads the entire XML into memory, so if the size of the XML is very huge, there might be problems using it (it might be better for smaller XML files). XmlTextReader perhaps uses paging, etc and therefore, it can even manage big XML files which don't fit entirely into the memory. But somebody told me that XmlDocument is slower than XmlTextReader. From what I said in this para, I guess that XmlDocument should be faster because it holds everything in memory?

Pls comment..

Thanks,
Mayank.
0
Comment
Question by:Mayank S
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
  • 2
  • +3
18 Comments
 
LVL 30

Author Comment

by:Mayank S
ID: 12214806
Is it like the same DOM parser vs SAX parser concept in Java?
0
 
LVL 34

Expert Comment

by:flavo
ID: 12214886
>> advantages

You can edit a XmlDocument, XmlTextReader is read only
0
 
LVL 30

Author Comment

by:Mayank S
ID: 12214893
>> You can edit a XmlDocument, XmlTextReader is read only

I also asked about XmlTextWriter :-) I know that the reader can only read. That's why I said:

>> XmlDocument against the XmlTextReader/ XmlTextWriter classes

:-)
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 34

Assisted Solution

by:flavo
flavo earned 25 total points
ID: 12214921
0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 12215181
XmlDocument is a complete read/write DOM. XmlReader and XmlWriter are sequential, e.g. they don't hold more than one node at the time in memory. Therefore, they are much more memory efficient, but harder to use in more complex cases. Also, when you have small XML documents which you run many queries on, you might want to look at the XPathDocument which is between the XmlDocument and the XmlReader: it holds the entire document in memory, but is much more memory-efficient (MS says about 3.4 times as far as I remember) and read-only, but you can run XPath queries agaist it.
0
 
LVL 30

Author Comment

by:Mayank S
ID: 12215227
>> Therefore, they are much more memory efficient

That follows from what you said: "they don't hold more than one node at the time in memory". They use less memory, all right. But does that not mean that they could also be slower.... they need to perform paging, etc all the time?
0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 12215413
No. They are forward-only, you cannot navigate through the XML document using the XmlReader, but only read it sequentially. In fact, both XmlDocument and XPathDocument do use XmlReader to read the XML content; you have to see the XmlReader as a XML parser which just splits the XML data into tokens. No paging or so involved.
0
 
LVL 30

Author Comment

by:Mayank S
ID: 12215424
>> No paging or so involved.

An, then my information and assumption were wrong. Then how do they manage cases where I load a large XML document and try to read it through a reader? (Keeping in mind - "they don't hold more than one node at the time in memory")

>> using the XmlReader

What's the difference b/w XmlReader and XmlTextReader?
0
 
LVL 10

Assisted Solution

by:ptmcomp
ptmcomp earned 25 total points
ID: 12215445
XmlDocument is an object model which allowes you to navigate through the tree and change values. XmlReader / XmlWriter directly read input / produce output on the fly. There is no object model and no navigating back. XmlReader / XmlWriter are fast and memory optimized while XmlDocument holds the whole document in memory and allowes complex operations on the model. XmlDocument uses XmlReader and XmlWriter for the load and save operations. XmlReader and XmlWriter are more low level API while XmlDocument is an object model for Xml data.
0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 12215464
They read one node after the other in a forward way. Imagine this XML document:
<root><child>sometext<empty /></child></root>
The XmlReader will return the following nodes, advancing using the Read() method, which moves to the next node if available:
NodeType: XmlElement, LocalName: root, IsEmptyElement: false
NodeType: XmlElement, LocalName: child, IsEmptyElement: false
NodeType: XmlText, Value: sometext
NodeType: XmlElement, LocalName: empty, IsEmptyElement: true
NodeType: XmlEndElement, LocalName: child
NodeType: XmlEndElement, LocalName: root
There is no way to move backwards or back to the upper level using the XmlReader! Therefore, all nodes which have been passed are no longer kept in memory.

XmlReader is an abstract base class which defines the interface for sequential reading of XML data. There are different XmlReader implementations around; XmlTextReader reads XML data from a (text) stream, XmlValidatingReader does the same but also validates the XML data. There are more, like the XmlNodeReader and custom readers by 3rd parties.
0
 
LVL 30

Author Comment

by:Mayank S
ID: 12215555
Yeah, I got it when you said forward-only. I know its got methods like Read () which read the node and advance to the next position, etc - the same age-old enumerator or iterator-like concept.

So - which one is faster?
0
 
LVL 14

Accepted Solution

by:
AvonWyss earned 30 total points
ID: 12215601
Well, if you can live with the forward-only limitation, the reader is faster - which is clear, since XmlDocument and XPathDocument do use the reader to load their XML data. If you need the ability to navigate, but don't neet to modify data, XPathDocument is the way to go. If you need read/write, use XmlDocument - but it's the slowest and most resource consuming of them all, which makes sense since it also has the most functionality.
0
 
LVL 2

Assisted Solution

by:Discofish
Discofish earned 20 total points
ID: 12215798
I normally use XmlDocument in places where the XML is small and does not require lots of parsing.  It speeds up development time without a noticeable hit on performance.
0
 
LVL 10

Assisted Solution

by:Hans Langer
Hans Langer earned 25 total points
ID: 12258760
Is the same relation  Dataset  vs DataReader
Dataset, XmlDocument: Both load an intelligent structure in memory, that mean is slow for big data source.
DataReader, XmlTextReader/ XmlTextWriter: Just Return what you asking for, without Intelligence.

Below code is the validation that do Load property of Xmldocument:

Public Overridable Sub Load(ByVal reader As XmlReader)
      Try
            Me.IsLoading = True
            Me.actualLoadingStatus = True
            Me.RemoveAll
            Me.fEntRefNodesPresent = False
            Me.fIsEdited = False
            Me.fCDataNodesPresent = False
            Me.loader.Load(Me, reader, Me.preserveWhitespace)
      Finally
            Me.IsLoading = False
            Me.actualLoadingStatus = False
      End Try
End Sub



0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 12258800
Mayank, is your question answered?
0
 
LVL 30

Author Comment

by:Mayank S
ID: 12306361
Sorry I forgot to close it earlier as I was occupied with other stuff.
0
 
LVL 14

Expert Comment

by:AvonWyss
ID: 12306367
No problem. Happy coding.
0
 
LVL 30

Author Comment

by:Mayank S
ID: 12306411
Thanks :-)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question