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..

LVL 30
Mayank SAssociate Director - Product EngineeringAsked:
Who is Participating?
AvonWyssConnect With a Mentor Commented:
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.
Mayank SAssociate Director - Product EngineeringAuthor Commented:
Is it like the same DOM parser vs SAX parser concept in Java?
>> advantages

You can edit a XmlDocument, XmlTextReader is read only
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Mayank SAssociate Director - Product EngineeringAuthor Commented:
>> 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

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.
Mayank SAssociate Director - Product EngineeringAuthor Commented:
>> 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?
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.
Mayank SAssociate Director - Product EngineeringAuthor Commented:
>> 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?
ptmcompConnect With a Mentor Commented:
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.
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.
Mayank SAssociate Director - Product EngineeringAuthor Commented:
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?
DiscofishConnect With a Mentor Commented:
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.
Hans LangerConnect With a Mentor Commented:
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)
            Me.IsLoading = True
            Me.actualLoadingStatus = True
            Me.fEntRefNodesPresent = False
            Me.fIsEdited = False
            Me.fCDataNodesPresent = False
            Me.loader.Load(Me, reader, Me.preserveWhitespace)
            Me.IsLoading = False
            Me.actualLoadingStatus = False
      End Try
End Sub

Mayank, is your question answered?
Mayank SAssociate Director - Product EngineeringAuthor Commented:
Sorry I forgot to close it earlier as I was occupied with other stuff.
No problem. Happy coding.
Mayank SAssociate Director - Product EngineeringAuthor Commented:
Thanks :-)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.