• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 418
  • Last Modified:

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.
0
Mayank S
Asked:
Mayank S
  • 7
  • 6
  • 2
  • +3
5 Solutions
 
Mayank SAssociate Director - Product EngineeringAuthor Commented:
Is it like the same DOM parser vs SAX parser concept in Java?
0
 
flavoCommented:
>> advantages

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

:-)
0
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
AvonWyssCommented:
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
 
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?
0
 
AvonWyssCommented:
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
 
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?
0
 
ptmcompCommented:
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
 
AvonWyssCommented:
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
 
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?
0
 
AvonWyssCommented:
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
 
DiscofishCommented:
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
 
Hans LangerCommented:
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
 
AvonWyssCommented:
Mayank, is your question answered?
0
 
Mayank SAssociate Director - Product EngineeringAuthor Commented:
Sorry I forgot to close it earlier as I was occupied with other stuff.
0
 
AvonWyssCommented:
No problem. Happy coding.
0
 
Mayank SAssociate Director - Product EngineeringAuthor Commented:
Thanks :-)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 7
  • 6
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now