Link to home
Start Free TrialLog in
Avatar of Mayank S
Mayank SFlag for India

asked on

XML

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.
Avatar of Mayank S
Mayank S
Flag of India image

ASKER

Is it like the same DOM parser vs SAX parser concept in Java?
>> advantages

You can edit a XmlDocument, XmlTextReader is read only
>> 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

:-)
SOLUTION
Avatar of flavo
flavo
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
>> 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.
>> 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?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Mayank, is your question answered?
Sorry I forgot to close it earlier as I was occupied with other stuff.
No problem. Happy coding.
Thanks :-)