Solved

XML

Posted on 2004-10-03
18
394 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:mayankeagle
  • 7
  • 6
  • 2
  • +3
18 Comments
 
LVL 30

Author Comment

by:mayankeagle
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:mayankeagle
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
 
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:mayankeagle
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:mayankeagle
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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:mayankeagle
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:mayankeagle
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:mayankeagle
ID: 12306411
Thanks :-)
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Wpf develop 5 35
Get String split 5 33
Copy/Clone an object. 9 17
Cordova Camera plugin fails 2 26
Preface In the first article: A Better Website Login System (http://www.experts-exchange.com/A_2902.html) I introduced the EE Collaborative Login System and its intended purpose. In this article I will discuss some of the design consideratio…
Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
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…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now