Which one is the best XmlDocument or XmlReader (in performance) for parsing quite large XML files (size or number of elements in it)
Asked
Active
Viewed 1,474 times
0
-
This is not a good way to ask. Please read [FAQ] and [ask] – Soner Gönül Oct 21 '13 at 06:46
-
Since they do different jobs, it's meaningless to compare their performance. – Michael Kay Oct 21 '13 at 07:21
2 Answers
3
The question shouldn't be which is faster but which is good for your case.
XmlDocument loads entire document into memory and allows you to modify it and query the content. After all you can save modified document back to file.
XmlReader provides read only and forward only access to the content of XML document, one element at the time.
You have to choose which description fits into your case.
You should also be aware that there is another way to handle XML documents in .NET, called LINQ to XML.

MarcinJuraszek
- 124,003
- 15
- 196
- 263
-
1So if i want to edit or add more elements to an XML file i will have to load all in the Memory ? Can i use XML to LINQ to edit XML files? – MrCSharp Oct 21 '13 at 06:55
-
Yes, you can. And I would really advice you to do so, because it's much easier to use than old XmlDocument. – MarcinJuraszek Oct 21 '13 at 06:58
-
And XmlDocument looses line numbers (e. g. useful for xsd schema validation) – jifb Jun 18 '21 at 09:39
-1
Another option maybe to load the XML into a dataset:
Public Function GetXMLDataset() As DataSet
Try
Dim ds As New DataSet
Dim xd As New XmlDocument
xd.LoadXml(txtXML.Text)
ds.ReadXml(New XmlNodeReader(xd), XmlReadMode.InferSchema)
Return ds
Catch ex As Exception
MsgBox(ex.Message)
Return Nothing
End Try
End Function
In this snippet the XML was already read into a textbox but you can develop other ways to get the XML.
You might want to define "quite larger" as reading the entire file may not be practical.

rheitzman
- 2,247
- 3
- 20
- 36
-
-1: `DataSet` doesn't work in all cases. In fact, it only works in the case where the XML matches the relational data model. – John Saunders Oct 21 '13 at 19:07
-
Nothing works in all cases, especially with "XML" data - you didn't exactly define the base conditions very well. A DateSet certainly will work in some cases. – rheitzman Oct 21 '13 at 19:24
-